Inheritance


Now you have a class that implements one method, and you know how to instantiate that class. One of the most powerful features of object-oriented programming is that you organize your classes into conceptual hierarchies, just like you can organize buildings into a conceptual hierarchy. One class can subclass another and when it does this, it inherits the members of the class from which it does the inheriting.

Although you are no doubt familiar with the idea of inheritance, object-oriented inheritance works a little differently than genetic inheritance does. First of all, people have two parents. I'm a mix of my dad and my mom; 50% of my gene pool comes from mom and 50% from dad. (I've often said that I have the distinct pleasure of having inherited all of my parents' worst qualities. In a remarkable reversal of fortune, my daughter has managed to inherit only the finest traits from her parents, mostly from her mom). REALbasic takes a simpler view of inheritance. There's only one parent, which is the superclass. In REALbasic, at the top of the family tree is the root class; Object and all other classes are subclasses of it.

In the object-oriented world, the child is called a subclass and the parent is referred to as the superclass. When you create a subclass in REALbasic, it automatically inherits all the methods and properties of the parent class. There is absolutely no difference other than their name.

Creating a subclass that doesn't do anything differently from the superclass is more or less a pointless exercise. The reason for creating a subclass is that you want to reuse the code you've written for the superclass, and then either add new capabilities or change the way certain things are done.

A child inherits all the features of the parent, but it can also add some new ones. In practice, this means that the subclass has all the methods, constants, and properties of the parent class (with a few exceptions), but it can also implement its own methods, constants, and properties. In this respect, at least, object-oriented inheritance isn't all that much different from the way parent-child relationships work with humans. That's why I always had to set the time on the VCR when I was growing up. My parents didn't know how to do it, but I did. In addition to the methods passed along by the parent, the child can have its own set of methods.

To create a subclass of Alpha in REALbasic, create a class as if it is a new class. Let's name it Beta, which can be done by typing the name into the Properties pane. There are two other properties listed in the pane that we have not discussed yet. After Name comes Interfaces, which I cover later, followed by Super. Setting the value of Super is how you designate this object's superclass, as shown in Figure 3.5. Type in Alpha, and you now have an official subclass of Alpha. It is identical in every way, except that it's called Beta instead of Alpha.

Figure 3.5. Define an object's superclass in the Properties pane.


Dim a as Alpha Dim b as Alpha a = New Alpha("Woohoo!") b = New Beta("Woohoo!") 


At this point, the only difference between a and b is their type (or class).




REALbasic Cross-Platform Application Development
REALbasic Cross-Platform Application Development
ISBN: 0672328135
EAN: 2147483647
Year: 2004
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net