Inheritance

From CS 61A Wiki
Revision as of 14:20, 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).

Motivation

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 methods with Plane.

def class Plane:                           def class Boeing_747:
   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
 
   def fly(self):                             def fly(self):
      print("Takeoff!")                          print("Takeoff!")
 
   def refuel(self):                          def refuel(self):
      self.fuel += 100                           self.fuel += 100
def class Plane:                           def 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   
      print("Takeoff!")                          
 
   def refuel(self):                          
      self.fuel += 100

In the first example, Plane and Boeing_747 exist as seperate classes, each with their own methods. Because they share fly and refuel, Boeing_747 can inherit from Plane and not have to list out those overlapping functions. The second example demonstrates what this looks like; Boeing_747 is now a subclass of Plane.

>>> wikiPlane = Boeing_747(25, 200)
>>> wikiPlane.fly()
Takeoff!
>>> wikiPlane.refuel()
>>> wikiPlane.fuel
100

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):
        super().__init__("rectangle")
 
class Square(Rectangle):
    def __init__(self, side):
	super().__init__(side, side)
        Shape.__init__(self, "square")

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

Method and constructor lookup

When the user creates an object, the constructor method is invoked. When an object calls a method, that corresponding method definition is invoked.

Method/attribute overriding