From CS 61A Wiki
Revision as of 15:02, 6 July 2014 by Axis (Talk | contribs)

Jump to: navigation, search

Inheritance is an OOP idea that specializes a class based on a general class. The specialized class inherits the attributes from the general class but has some special-case behavior. The general class is called the superclass (or parent class or base class), while the specialized class is called the subclass (or child class).


The concept of inheritance stems from the desire to reduce redundancy. Any class that has the same functionality and attributes as a more general class should inherit those traits, as opposed to creating them again. The Boeing_747 class should, for instance, inherit from the Plane class, because the Boeing_747 class shares many functions with Plane.

class Plane:                                 class Boeing_747:
    def __init__(self, num_passengers):          def __init__(self, num_first_class, num_economy):
        ...                                          ...
    def fly(self)                                def fly(self):
        ...                                          ...
    def refuel(self)                             def refuel(self):
        ...                                          ...

class Plane:                               class Boeing_747(Plane): 
    def __init__(self, num_passengers):        def __init__(self, num_first_class, num_economy):
        ...                                        ...
                                           # (Plane) after Boeing_747 indicates that Boeing_747
    def fly(self):                         # inherits from Plane                            
    def refuel(self):

In the first example, Plane and Boeing_747 exist as separate classes, each with their own functions. Because they share fly and refuel, Boeing_747 can inherit from Plane and not have to list out the overlapping functions. In the second example, Boeing_747 is now a subclass of Plane.

Inheritance allows users to create branching, related systems of classes instead of individual static classes. Because inherited functions and variables are controlled from the super class, the user simply needs to change the inherited values and they will be reflected in all subclasses that inherit from the super class.

Inheritance vs. composition

Inheritance represents an "is-a" relationship between classes. For example, a Rectangle is a Shape, and a Square is a Rectangle:

class Shape:
    def __init__(self, type):
        self.type = type
class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
class Square(Rectangle):
    def __init__(self, side):
	super().__init__(side, side)
        Shape.__init__(self, "square")

Shape is the superclass of Rectangle, and Rectangle is the superclass of Square. A Shape has a type, which is set to "rectangle" and "square" for Rectangle and Square, respectively. A Rectangle has its own attributes, length and width. Square inherits all its attributes from Rectangle and sets its own type.

Composition represents a "has-a" relationship between classes and objects. For example, a Line has endPoints:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
class Line:
    def __init__(self, point1, point2):
        self.start = point1
        self.end = point2

Function and constructor lookup

A subclass that doesn't include a certain function, can lookup the function in its super class.

class Plane:                               class Boeing_747(Plane): 
   def __init__(self, num_Passengers):        def __init__(self, num_First_Class num_Economy):
      self.p = num_Passengers                    self.f = num_First_Class
      self.fuel = 0                              self.e = num_Economy
                                                 self.fuel = 0
                                           # (Plane) after Boeing_747 indicates that Boeing_747
   def fly(self):                          # inherits from Plane   
   def refuel(self):                          
      self.fuel += 100

>>> wikiPlane = Boeing_747(25, 200)
>>> wikiPlane.refuel()
>>> wikiPlane.fuel

Here a Boeing_747 object is created, and the user has the Boeing_747 object call fly and refuel. Although the class Boeing_747 does not define fly and refuel, it looks into its super class Plane and calls the functions defined there.

Function/attribute overriding

In the case that a subclass wants to alter a function in its superclass, a subclass can override a superclass's function.

Multiple inheritance

A class may inherit from multiple superclasses. When calling methods, the method resolution order (MRO) is used to check which method to call. For example, we define the following classes:

class A:
    def me(self):
class B:
    def me(self):
class C(A, B):

C inherits from both A and B. Since C does not define a method me, Python checks the superclasses left to right, i.e., A then B. Since A defines a method me, Python calls me in A. Here is the result and the MRO for C:

>>> C().me()
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)