Introduction
Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows one class to inherit the attributes and methods of another class. Instead of writing the same code repeatedly, you can create a new class that builds upon an existing one.
The existing class is called the parent class (or base class), while the new class is called the child class (or derived class). The child class automatically receives the properties and methods of its parent and can also add its own features.
Why Use Inheritance?
Inheritance makes programs easier to maintain by reducing duplicated code. It also makes applications easier to extend because new classes can reuse existing functionality instead of starting from scratch.
- Promotes code reuse.
- Reduces duplicated code.
- Makes programs easier to maintain.
- Allows existing classes to be extended.
- Supports hierarchical program design.
Parent and Child Classes
Suppose we have a parent class called Animal. Every animal has a name and can make a sound.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("The animal makes a sound.")
We can now create a child class that inherits everything from the parent class.
class Dog(Animal):
pass
The Dog class now automatically inherits the constructor and the speak() method from the Animal class.
Creating an Object from the Child Class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("The animal makes a sound.")
class Dog(Animal):
pass
dog = Dog("Buddy")
print(dog.name)
dog.speak()
Output:
Buddy
The animal makes a sound.
Overriding Parent Methods
A child class can replace a method inherited from its parent. This is called method overriding. It allows the child class to provide its own implementation while keeping the same method name.
class Animal:
def speak(self):
print("The animal makes a sound.")
class Dog(Animal):
def speak(self):
print("The dog barks.")
dog = Dog()
dog.speak()
Output:
The dog barks.
Using the super() Function
The super() function allows a child class to call methods from its parent class. It is commonly used to execute the parent's constructor before adding new attributes.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
dog = Dog("Buddy", "Labrador")
print(dog.name)
print(dog.breed)
Output:
Buddy
Labrador
Adding New Methods
A child class is not limited to the methods inherited from its parent. It can define additional methods that exist only in the child class.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def wag_tail(self):
print(self.name, "is wagging its tail.")
dog = Dog("Buddy")
dog.wag_tail()
Output:
Buddy is wagging its tail.
Creating Multiple Child Classes
A single parent class can have many child classes. Each child inherits the common features while implementing its own behavior.
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal):
def speak(self):
print("Bark")
class Cat(Animal):
def speak(self):
print("Meow")
dog = Dog()
cat = Cat()
dog.speak()
cat.speak()
Output:
Bark
Meow
Real-World Example
Inheritance is commonly used when several objects share the same characteristics but also have their own unique features. In the example below, both cars and motorcycles are vehicles, so they inherit common information from the Vehicle class while adding their own methods.
class Vehicle:
def __init__(self, brand):
self.brand = brand
def start(self):
print(self.brand, "is starting.")
class Car(Vehicle):
def open_trunk(self):
print("Opening the trunk.")
class Motorcycle(Vehicle):
def wheelie(self):
print("Performing a wheelie.")
car = Car("Toyota")
bike = Motorcycle("Honda")
car.start()
car.open_trunk()
bike.start()
bike.wheelie()
Output:
Toyota is starting.
Opening the trunk.
Honda is starting.
Performing a wheelie.
Advantages of Inheritance
- Encourages code reuse.
- Reduces duplicated code.
- Makes applications easier to maintain.
- Improves readability and organization.
- Makes it easier to extend existing programs.
- Supports scalable object-oriented design.
Best Practices
- Use inheritance only when there is a genuine "is-a" relationship between classes.
- Keep parent classes general and reusable.
- Override methods only when necessary.
- Use
super()to reuse the parent constructor. - Avoid creating deep inheritance hierarchies unless they are truly needed.
Summary
Inheritance allows one class to inherit attributes and methods from another class, making Python programs cleaner, more reusable and easier to maintain. By combining inheritance with classes and objects, you can build applications that are flexible, scalable and much easier to extend as your projects grow.
Examples
The following examples help reinforce the concepts explained in this lesson.
<!DOCTYPE html>
<html>
<head>
<title>My First Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
💡 Pro Tip
Practice every concept immediately after reading it. Learning by doing is the fastest way to master HTML.
⚠ Common Mistake
Do not simply copy code examples. Type them yourself and experiment with small changes.
Best Practices
- Write clean and readable HTML.
- Indent your code consistently.
- Use semantic HTML elements.
- Validate your HTML regularly.
- Test your pages in multiple browsers.
Frequently Asked Questions
Why should I learn HTML first?
HTML is the foundation of every website. Once you understand HTML, learning CSS and JavaScript becomes much easier.
Is HTML difficult?
No. HTML is considered one of the easiest web technologies to learn, making it an excellent starting point for beginners.