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 seperate classes, each with their own functions. 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
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 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.
In the case that a subclass wants to alter a function in its superclass, a subclass can override a superclass's function.
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): print("Called A.me") class B: def me(self): print("Called B.me") class C(A, B): pass C().me()
C inherits from both
C does not define a method
me, Python checks the superclasses left to right, i.e.,
A defines a method
me, Python calls
A. Here is the result and the MRO for
>>> C().me() Called A.me >>> C.__mro__ (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)