Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Inheritance (OOP) quickly and effectively.
484+ students studying
Inheritance is a key principle of Object-Oriented Programming (OOP) that enables a new class to inherit attributes and methods from an existing class. The main purpose of inheritance is to promote code reuse and hierarchical relationships between classes, which helps organise and simplify code. With inheritance, programmers can create specialised classes based on more general classes, building on existing functionality without duplicating code.
Example:
class Animal: # Parent class
def speak(self):
print("The animal makes a sound.")
class Dog(Animal): # Child class
pass
Here, Animal is the parent class, and Dog is a child class that inherits from Animal. Although Dog has no methods defined in it, it inherits speak from Animal.
In Python, inheritance is established by passing the parent class as a parameter to the child class.
Example:
class Dog(Animal): # Dog inherits from Animal
def bark(self):
print("Woof woof!")
Here, Dog inherits everything from Animal but also has its own unique method, bark.
Definition: Method overriding occurs when a child class provides a new implementation for a method that is already defined in the parent class. This allows the child class to modify or specialise the behaviour of an inherited method.
Example:
class Animal:
def speak(self):
print("The animal makes a sound.")
class Dog(Animal):
def speak(self): # Overriding speak method
print("Woof woof!")
In this example, Dog has its own version of speak, which overrides the speak method of the Animal class.
super()
Purpose: The super() function allows the child class to access methods or attributes from the parent class. This is especially useful when a child class overrides a method but still needs to use functionality from the parent class's version of that method.
Example:
class Animal:
def __init__(self, species):
self.species = species
class Dog(Animal):
def __init__(self, species, breed):
super().__init__(species) # Calls the parent class's constructor
self.breed = breed
In this example, super().init(species) calls the init method from Animal, ensuring that the species attribute is correctly initialised.
Below is an example demonstrating inheritance in an animal class hierarchy, where Animal is a general parent class, and Dog and Cat are more specific child classes.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "The animal makes a sound."
class Dog(Animal):
def speak(self): # Overriding speak method
return "Woof woof!"
class Cat(Animal):
def speak(self): # Overriding speak method
return "Meow!"
# Creating instances of each class
animal = Animal("Generic Animal")
dog = Dog("Buddy")
cat = Cat("Whiskers")
# Using inherited and overridden methods
print(animal.speak()) # Outputs: The animal makes a sound.
print(dog.speak()) # Outputs: Woof woof!
print(cat.speak()) # Outputs: Meow!
Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
70 flashcards
Flashcards on Inheritance (OOP)
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards7 quizzes
Quizzes on Inheritance (OOP)
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Inheritance (OOP)
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Inheritance (OOP)
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Inheritance (OOP)
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Inheritance (OOP) to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered