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 methods 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 seperate classes, each with their own methods. Because they share
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
class Shape: def __init__(self, type): self.type = type class Rectangle(Shape): def __init__(self, length, width): super().__init__("rectangle") 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 is the superclass of
Shape has a type, which is set to "rectangle" and "square" for
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 end
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
A subclass that doesn't include a certain method, can lookup the method 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 print("Takeoff!") def refuel(self): self.fuel += 100
>>> wikiPlane = Boeing_747(25, 200) >>> wikiPlane.fly() Takeoff! >>> wikiPlane.refuel() >>> wikiPlane.fuel 100
Here a Boeing_747 object is created, and the user has the Boeing_747 object call
refuel. Although the class Boeing_747 does not define
refuel, it looks into its super class
Plane and calls the functions defined there.