Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Derivation and Inheritance

This section reviews the concept of a derived class. Derived classes are an extremely useful feature of Python because they allow the programmer to define new classes by extending existing classes. By using derived classes, the programmer can exploit the commonalities that exist among the classes in a program. Different classes can share values and operations.

Derivation  is the definition of a new class by extending an existing class. The new class is called the derived class  and the existing class from which it is derived is called the base class .

In Python there must be at least one base class, but there may be more than one base class (multiple inheritance  ).

Python supports so-called classic classes   and new-style classes  . A new-style class is one that is ultimately derived from the built-in object class. A classic class is one that does not have a base class or one that is derived only from other classic classes. Classic classes are being phased out of the Python langauge and are not used in this book.

Consider the Person class defined in Program gif and the Parent class defined in Program gif. Because parents are people too, the Parent class is derived from the Person class. Derivation in Python is indicated by including the name(s) of the base class(es) in parentheses in the declaration of the derived class.

   program57168
Program: Person class.

   program57179
Program: Parent class.

A derived class inherits  all the attributes of its base class. That is, the derived class contains all the class attributes contained in the base class and the derived class supports all the same operations provided by the base class. For example, consider the following statements:

p = Person()
q = Parent()
Since p is a Person, it has the instance attributes _name and _sex and method __str__. Furthermore, since Parent is derived from Person, then the object q also has the instance attributes _name and _sex and method __str__.

A derived class can extend the base class in several ways: New instance attributes can be used, new methods can be defined, and existing methods can be overridden . For example, the Parent class adds the instance attribute _children and the method getChild.

If a method is defined in a derived class that has the same name as a method in a base class, the method in the derived class overrides  the one in the base class. For example, the __str__ method in the Parent class overrides the __str__ method in the Person class. Therefore, str(p) invokes Person.__str__, whereas str(q) invokes Parent.__str__.

An instance of a derived class can be used anywhere in a program where an instance of the base class may be used. For example, this means that a Parent may be passed as an actual parameter to a method that expects to receive a Person.


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.