Inheritance

Comprehensive study notes, diagrams, and exam preparation for Inheritance.

Inheritance

Definition

Inheritance is the mechanism in object-oriented programming by which one class, called the child class or derived class, acquires the properties, methods, and behavior of another class, called the parent class or base class.

It represents an “is-a” relationship, meaning the child class is a specialized form of the parent class. For example, a “Dog” is an “Animal,” so Dog can inherit common characteristics from Animal.

Inheritance may include:

  • Data members or attributes
  • Methods or functions
  • Behavior and structure
  • Modifications or additions in the child class

This allows existing code to be reused efficiently while enabling customization and expansion.


Main Content

1. Parent Class and Child Class

  • The parent class, also known as the base class or superclass, contains the common properties and behaviors shared by multiple related objects.
  • The child class, also known as the derived class or subclass, inherits those features and can also define its own additional features.

The parent class acts as the general blueprint, while the child class is a more specific version of that blueprint.

Example:

class Animal:
    def eat(self):
        print("Animal eats food")

class Dog(Animal):
    def bark(self):
        print("Dog barks")

d = Dog()
d.eat()
d.bark()

In this example:

  • Animal is the parent class
  • Dog is the child class
  • Dog inherits the eat() method from Animal
  • Dog also has its own method bark()

This structure reduces duplication. Instead of writing eat() in every animal type, it is written once in the parent class and reused by all child classes.

A simple hierarchy can be understood like this:

        Animal
           |
    ----------------
    |              |
   Dog            Cat

This means both Dog and Cat share features of Animal, but each can also have unique behaviors.


2. Types of Inheritance

  • Single inheritance: one child class inherits from one parent class.
  • Multiple inheritance: one child class inherits from more than one parent class.
  • Multilevel inheritance: a class inherits from a class that already inherited from another class.
  • Hierarchical inheritance: multiple child classes inherit from the same parent class.
  • Hybrid inheritance: a combination of two or more types of inheritance.

Single Inheritance

In single inheritance, the relationship is simple and direct.

class Person:
    def show(self):
        print("This is a person")

class Student(Person):
    def study(self):
        print("Student studies")

s = Student()
s.show()
s.study()

Multiple Inheritance

A class can inherit features from more than one parent.

class A:
    def func1(self):
        print("A")

class B:
    def func2(self):
        print("B")

class C(A, B):
    pass

Multilevel Inheritance

Inheritance happens in levels.

Animal -> Mammal -> Dog

Here:

  • Dog inherits from Mammal
  • Mammal inherits from Animal
  • Dog indirectly gets Animal’s properties too

Hierarchical Inheritance

One parent class serves as the base for many child classes.

          Shape
         /     \
      Circle   Square

Hybrid Inheritance

This is a combination of two or more inheritance patterns. It is often used in complex programs where different relationships are needed.

These inheritance types help programmers choose the most suitable structure for a problem. The correct type depends on how the real-world relationship is modeled and how the code should be organized.


3. Method Overriding and Superclass Access

  • Method overriding occurs when a child class provides its own version of a method already defined in the parent class.
  • Superclass access allows the child class to use the parent’s version of a method or constructor when needed.

Method overriding is useful when the child class needs behavior that is more specific than the parent class behavior.

Example:

class Animal:
    def sound(self):
        print("Animal makes a sound")

class Dog(Animal):
    def sound(self):
        print("Dog barks")

a = Animal()
d = Dog()
a.sound()
d.sound()

Here:

  • Animal.sound() prints a general message
  • Dog.sound() replaces it with a more specific one

Sometimes the child class still wants to use the parent class’s behavior. In many languages, this is done using a keyword such as super().

Example:

class Parent:
    def __init__(self):
        print("Parent constructor")

class Child(Parent):
    def __init__(self):
        super().__init__()
        print("Child constructor")

This is important because:

  • It allows the child to extend parent functionality instead of fully replacing it
  • It helps initialize inherited data properly
  • It avoids repeating code unnecessarily

Inheritance is not just about sharing data; it is also about controlling how inherited behavior is adapted.


Working / Process

  1. A base class is created with common features. The programmer first defines the general class that contains shared attributes and methods. This class is usually built from the common characteristics found in related objects.

  2. A derived class is declared to inherit from the base class. The child class is linked to the parent class using inheritance syntax. After this, the child automatically receives the accessible properties and methods of the parent class.

  3. The child class uses, extends, or overrides inherited members. The child can directly use inherited methods, add new methods, or replace parent methods with more specific behavior. If needed, it can also call the parent version using superclass access.

  4. Objects are created from the child class or parent class. When an object is created, it can access inherited functionality according to access rules. The child object behaves as both the child type and the parent type in many cases.

  5. The program executes inherited and specialized behavior together. The final result is a structured system where general code is reused and specific code is added only where needed.

Example process:

class Employee:
    def details(self):
        print("Employee details")

class Manager(Employee):
    def manage(self):
        print("Manager manages team")

m = Manager()
m.details()
m.manage()

Here the process works as:

  • Employee provides the common function
  • Manager inherits it
  • Manager adds a new function
  • The object m can use both

Advantages / Applications

  • Code reusability: Common features are written once in the parent class and reused by many child classes, reducing duplication and making maintenance easier.
  • Better organization: Inheritance helps create a clear class hierarchy, making programs easier to understand, modify, and expand.
  • Real-world modeling: It closely matches real-life relationships such as Animal–Dog, Vehicle–Car, and Person–Student, making software design more natural and meaningful.

Inheritance is applied in many areas:

  • In software development for building reusable class structures
  • In graphical user interfaces where base widgets are extended into specialized controls
  • In game development for creating character and object hierarchies
  • In simulation systems for modeling categories and subcategories
  • In frameworks and libraries where common functionality is shared through base classes

Example of application:

class Vehicle:
    def move(self):
        print("Vehicle moves")

class Car(Vehicle):
    def move(self):
        print("Car drives")

class Bike(Vehicle):
    def move(self):
        print("Bike rides")

This structure is useful because all vehicles share movement, but each type behaves differently. Such design supports flexibility and easy expansion.


Summary

  • Inheritance is a way for one class to acquire features from another class.
  • It helps create reusable and well-organized code.
  • It is based on the relationship between parent and child classes.
  • Important terms to remember: parent class, child class, base class, derived class, method overriding, superclass, subclass