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 inheritance and polymorphism -- two of the four pillars of Object-Oriented Programming -- as required by the OCR A-Level Computer Science (H446) specification. These concepts enable code reuse, extensibility, and flexible design.
Inheritance is the mechanism by which one class (the subclass or child class) can inherit the attributes and methods of another class (the superclass or parent class). The subclass can then extend or modify the inherited behaviour.
Inheritance models an "is-a" relationship:
| Term | Definition |
|---|---|
| Superclass (parent/base class) | The class being inherited from. |
| Subclass (child/derived class) | The class that inherits from the superclass. |
| Inheritance | The mechanism of one class acquiring the attributes and methods of another. |
In single inheritance, a subclass inherits from exactly one superclass.
CLASS Animal
PRIVATE name: STRING
PRIVATE age: INTEGER
PUBLIC PROCEDURE new(n: STRING, a: INTEGER)
name = n
age = a
END PROCEDURE
PUBLIC FUNCTION getName() RETURNS STRING
RETURN name
END FUNCTION
PUBLIC PROCEDURE speak()
OUTPUT name + " makes a sound"
END PROCEDURE
END CLASS
CLASS Dog INHERITS Animal
PRIVATE breed: STRING
PUBLIC PROCEDURE new(n: STRING, a: INTEGER, b: STRING)
SUPER.new(n, a)
breed = b
END PROCEDURE
PUBLIC PROCEDURE speak()
OUTPUT getName() + " barks!"
END PROCEDURE
PUBLIC FUNCTION getBreed() RETURNS STRING
RETURN breed
END FUNCTION
END CLASS
class Animal:
def __init__(self, name: str, age: int):
self.__name = name
self.__age = age
def get_name(self) -> str:
return self.__name
def speak(self):
print(f"{self.__name} makes a sound")
class Dog(Animal):
def __init__(self, name: str, age: int, breed: str):
super().__init__(name, age) # Call superclass constructor
self.__breed = breed
def speak(self): # Override the speak method
print(f"{self.get_name()} barks!")
def get_breed(self) -> str:
return self.__breed
# Usage
dog = Dog("Rex", 5, "Labrador")
dog.speak() # Rex barks! (overridden method)
print(dog.get_name()) # Rex (inherited method)
print(dog.get_breed())# Labrador (subclass method)
Multiple inheritance occurs when a subclass inherits from more than one superclass. This allows a class to combine features from multiple parent classes.
class Flyable:
def fly(self):
print("Flying...")
class Swimmable:
def swim(self):
print("Swimming...")
class Duck(Flyable, Swimmable):
def quack(self):
print("Quack!")
duck = Duck()
duck.fly() # Flying...
duck.swim() # Swimming...
duck.quack() # Quack!
The diamond problem arises when a class inherits from two classes that both inherit from the same superclass. This creates ambiguity about which version of a method to use.
A
/ \
B C
\ /
D
If both B and C override a method from A, which version does D inherit? Different languages handle this differently:
| Language | Solution |
|---|---|
| Python | Method Resolution Order (MRO) -- follows C3 linearisation |
| Java | Does not allow multiple class inheritance; uses interfaces instead |
| C++ | Virtual inheritance resolves ambiguity |
Exam Tip: You must be able to explain the diamond problem and describe how different languages resolve it. Python uses MRO; Java avoids the problem entirely by prohibiting multiple class 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):
return 0
class Circle(Shape):
def __init__(self, radius: float):
self.__radius = radius
def area(self): # Overrides Shape.area()
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): # Overrides Shape.area()
return self.__width * self.__height
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.