13.9. Subclassing and Derivation

Composition works fine when classes are distinct and are a required component of larger classes, but when you desire “the same class but with some tweaking,” derivation is a more logical option.

One of the more powerful aspects of OOP is the ability to take an already-defined class and extend it or make modifications to it without affecting other pieces of code in the system that use the currently-existing classes. OOD allows for class features to be “inherited” by “descendant” classes or “subclasses.” These subclasses “derive” the core of their attributes from “base” (a.k.a. ancestor, super) classes. In addition, this derivation may be extended for multiple generations. Classes involved in a one-level derivation (or are adjacent vertically in a class tree diagram) have a “parent” and “child” class relationship. Those classes which derive from the same parent (or are adjacent horizontally in a class tree diagram) have a “sibling” relationship. Parent and all higher-level classes are considered ancestors.

Using our example from the previous section, let us imagine having to create different types of address books. We are talking about more than just creating multiple instances of address books—in this case, all objects have everything in common. What if we wanted a BusinessAddressBook class whose entries would contain more work-related attributes such as job position, phone number, and e-mail address? This would differ from a PersonalAddressBook class which would contain more family-oriented information such as home address, relationship, birthday, etc.

For both of these cases, we do not want to design these classes from scratch, because it would duplicate the work already accomplished to create the generic AddressBook class. Wouldn't it be nice to subsume all the features and characteristics of the AddressBook class and add specialized customization for your new, yet related, classes? This is the entire motivation and desire for class derivation.

13.9.1. Creating Subclasses

As we have seen earlier, the general syntax for declaring a base class looks like this:

							class
							ClassName:
     'optional class documentation string'
     class_suite
						

Derived classes are declared much like their parent class; however, a list of base classes to inherit from are given after the class name:

							class
							SubClassName (ParentClass1[, ParentClass2, …]):
     'optional class documentation string'
     class_suite
						

We have already seen some examples of classes and subclasses so far, but here is another simple example:

>>> class Parent:                 # define parent class
…       def parentMethod(self):
…           print 'calling parent method'

>>> p = Parent()                  # instance of parent
>>> dir(Parent)                   # parent class attributes
['__doc__', '__module__', 'parentMethod']
>>> p.parentMethod()
calling parent method
>>>
>>> class Child(Parent):          # define child class
…       def childMethod(self):
…           print 'calling child method'

>>> c = Child()                   # instance of child
>>> dir(Child)                    # child class attributes
['__doc__', '__module__', 'childMethod']
>>> c.childMethod()               # child calls its method
calling child method
>>> c.parentMethod()              # calls parent's method
calling parent method

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset