Servoy Tutorial: Multiple Inheritance

Servoy Tutorial: Multiple Inheritance

Servoy Tutorial Photo Credit: Thomas Hawk via Compfight

This is an object-oriented Servoy tutorial on how to use multiple inheritance with Servoy. If you read the article on “Prototypal Inheritance”, then you learned all about cloning objects using a standard function called objectPlus. This function cloned an object with all its properties and functions, and returned a new object to us, which we could then modify and use to clone new objects from. We applied this approach to a car dealership, creating a vehicle constructor function with basic properties, modifying it with a car constructor function, and then finally making a sports car object for our sexy corvette.

In this article, we will introduce a  new standard method called objectMulti, that takes any number of objects, and makes a new object that contains all the properties and functions from all the original objects. In other words, you could have three separate literal objects, and combine them all into one new object, with this single function. Very slick indeed!

In real programming scenarios, different objects may would contain properties and functions that represent different features and capabilities. We can pick and choose from our library, combining the objects to make one super object that inherits all the properties and functions from the individual objects. It’s kind of like a recipe to make something new from individual ingredients, where the ingredients are the individual objects.

Okay, here is the objectMulti function. Copy and paste this code into your utility library. You will find a use for it, trust me.

First we make the vehicle constructor function. Simple; use and object literal and add some properties. We could add methods to, but let’s not push ourselves, okay? Also, like in the Servoy tutorial on prototypal inheritance, we are not going to concern ourselves with which object has which properties. It really doesn’t matter; I’m adding them and overwriting them for demonstration purposes only; this is not a tutorial on designing objects to handle the needs of car dealerships.
Then we make the car constructor function. Again, straight forward using an object literal.
Finally, we make the sports car constructor function, and go all out, adding a function that will build a string from the properties we know this object will inherit from car and vehicle.
Whew! That was tough, right? Not! Okay, let’s use the constructors to build a new Corvette. First we create a new vehicle using the vehicle constructor function. Then we create a new car using the car constructor function. Finally, we build a new sports car using the sports car constructor function. Next, we pass the vehicle object, car object and sports car object, along with values for properties, into objectMulti, which combines them all, returning to us a new Corvette object. Pretty cool, right?
As you can see, using multiple inheritance is quite easy with a function like objectMulti. Put together objects from your collection and get a new object that inherits from all the objects. It’s very handy in real-world programming situations and works great with Servoy. Enjoy!

Gary Dotzlaw
Gary Dotzlaw has 20+ years of experience as a professional software developer and has worked with over 100 companies throughout the USA and Canada. Gary has extensive qualifications in all facets of the project life cycle, including initial feasibility analysis, conceptual design, technical design, development, implementation and deployment. Gary is an expert Servoy Developer, with extensive large-commercial project experience, and has written numerous advanced tutorials on Servoy development.