Difference between revisions of "Inheritance"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][unchecked revision]
m (add term)
(Motivation)
Line 2: Line 2:
  
 
== Motivation ==
 
== 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.
  
=== Compared with composition ===
+
<syntaxhighlight lang="python">
 +
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
  
== Method and constructor lookup ==
+
  def fly(self):                            def fly(self):
 +
      print("Takeoff!")                          print("Takeoff!")
 +
 
 +
  def refuel(self):                          def refuel(self):
 +
      self.fuel += 100                          self.fuel += 100
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="python">
 +
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                         
 +
</syntaxhighlight>
 +
 
 +
In the first example, Plane and Boeing_747 exist as seperate classes, each with their own methods. Because they share <code>fly</code> and <code>refuel</code>, 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.
 +
 
 +
=== Compared with composition ===
  
 +
== 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 ==
 
== Method/attribute overriding ==

Revision as of 14:07, 6 July 2014

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.

Compared with composition

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