You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers two of the four pillars of OOP: inheritance and polymorphism. These concepts allow programmers to build hierarchies of related classes, promote code reuse, and write flexible programs that can work with objects of different types through a common interface.
Inheritance is the mechanism by which one class (the subclass or child class) can acquire the attributes and methods of another class (the superclass or parent class). The subclass can then extend or modify the inherited behaviour.
| Term | Definition |
|---|---|
| Superclass (Parent class) | The class being inherited from. |
| Subclass (Child class) | The class that inherits from the superclass. |
| Base class | Another term for superclass, especially the top-level class in a hierarchy. |
| Derived class | Another term for subclass. |
| "Is-a" relationship | Inheritance models an "is-a" relationship: a Dog is an Animal. |
CLASS Animal
PRIVATE name: STRING
PRIVATE sound: STRING
PUBLIC PROCEDURE new(n: STRING, s: STRING)
name = n
sound = s
END PROCEDURE
PUBLIC FUNCTION getName() RETURNS STRING
RETURN name
END FUNCTION
PUBLIC PROCEDURE speak()
OUTPUT name + " says " + sound
END PROCEDURE
END CLASS
CLASS Dog INHERITS Animal
PRIVATE breed: STRING
PUBLIC PROCEDURE new(n: STRING, b: STRING)
SUPER.new(n, "Woof")
breed = b
END PROCEDURE
PUBLIC FUNCTION getBreed() RETURNS STRING
RETURN breed
END FUNCTION
PUBLIC PROCEDURE fetch()
OUTPUT getName() + " fetches the ball!"
END PROCEDURE
END CLASS
class Animal:
def __init__(self, name: str, sound: str):
self.__name = name
self.__sound = sound
def get_name(self) -> str:
return self.__name
def speak(self):
print(f"{self.__name} says {self.__sound}")
class Dog(Animal):
def __init__(self, name: str, breed: str):
super().__init__(name, "Woof")
self.__breed = breed
def get_breed(self) -> str:
return self.__breed
def fetch(self):
print(f"{self.get_name()} fetches the ball!")
rex = Dog("Rex", "Labrador")
rex.speak() # Output: Rex says Woof
rex.fetch() # Output: Rex fetches the ball!
Exam Tip: When explaining inheritance, always mention the "is-a" relationship. For example, "A Dog is an Animal" is valid inheritance, but "A Car has an Engine" is composition (a "has-a" relationship), not inheritance.
Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The subclass version replaces the superclass version for objects of the subclass type.
class Shape:
def area(self) -> float:
return 0.0
def describe(self):
print(f"This shape has an area of {self.area()}")
class Circle(Shape):
def __init__(self, radius: float):
self.__radius = radius
def area(self) -> float:
return 3.14159 * self.__radius ** 2
class Rectangle(Shape):
def __init__(self, width: float, height: float):
self.__width = width
self.__height = height
def area(self) -> float:
return self.__width * self.__height
c = Circle(5)
r = Rectangle(4, 6)
c.describe() # This shape has an area of 78.53975
r.describe() # This shape has an area of 24
Notice that describe() is defined in Shape but calls self.area(), which is overridden in each subclass. This is polymorphism in action.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.