Inheritance and Polymorphism

Inheritance and polymorphism are the two defining characteristics of object-oriented languages. Inheritance is the ability of an object class to inherit the operations and properties of its parent class. Polymorphism is the ability of a single-named operation to work on different values of different object classes. We'll explore them here to see how they are used in 3ds Max and MAXScript.

First and foremost, both characteristics depend on the system having a well-defined type, or class, for every value. Assigning classes to values divides all the values you can work with into groups, each of which have well-defined operations and properties for their values. In some languages, like standard C++, the class is only manifest at compile-time. In others, like Smalltalk and MAXScript, the classes are actual runtime entities and every value has a runtime class-tag that says what class it is. This is the main reason MAXScript can have type-free variables. It can look at the class-tag of the value in a variable at runtime to determine its class.

Once the objects are grouped into classes, the groups themselves can be arranged into hierarchies. We do this all the time in real life. A classic example is the biological hierarchy, arranging real-life objects hierarchically into animals and plants and mammals and vertebrates and humans and adults and children, and so on. An object-oriented language provides a way to arrange value classes into hierarchies and, like the hierarchies we are used to, a class at some point in the hierarchy shares all the operations and properties of all its parent classes. This sharing is known as inheritance in object-oriented languages, and the operations and properties defined for a class are automatically inherited by all of its descendant classes.

As an example, the following is the hierarchy for the Box class:

   Value
     MAXWrapper
       Node
         GeometryClass
           Box

Various characteristics are defined for a class, and each lower class inherits those characteristics. For example, the classOf() method is defined for class Value, and class Box inherits this method through its hierarchy. Thus you can specify a Box object as a parameter to the classOf() method.

In the MAXScript Class Hierarchy topic, all the built-in classes in MAXScript are laid out in their place in the hierarchy so you can see for each class the classes it inherits characteristics from. Some of the hierarchy comes from within 3ds Max itself which is internally object-oriented. All of its objects, modifiers, controllers, and so on are arranged in a well-defined inheritance hierarchy.

Once the objects have been grouped into classes, the symbology for the operations that can be performed on objects of each class can be simplified. The classic example of this comes from mathematics. The symbolic operators '+', '-', '', and '/' are shared among many types of values (integers, reals, complex, vectors, matrices, and so on), and perform the correct action on the types they are applied to. This capability for a single-named operation to work on different types is called *polymorphism. Object-oriented languages let you use the same name for different methods or operators across different classes, and the correct method to apply is automatically determined based on the class of the value it is used with.

In MAXScript, all math operators and methods are polymorphic in this sense and reflect the standard mathematical conventions. An interesting example for 3ds Max use is the random() method. It takes two arguments and generates a random value between them. So, if you give it floats it gives you a float back; if you give it 3D points, it gives you a random point in the box they define the corners of; if you give it quaternions, it gives you back a random rotation between them, and so on.

Further, all the methods that work on 3ds Max objects are polymorphic within their hierarchies. So move, hide, and select work on all types of scene objects; time scaling and insertion work on all the types of controllers, and so on.