Inheritance

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

Inheritance

Definition

Inheritance is an object-oriented programming mechanism in which a new class, called a derived class or subclass, acquires the properties and behaviors of an existing class, called a base class or superclass. It allows code reusability, hierarchical classification, and extension of existing functionality without rewriting code.

In simple terms, inheritance means “a child class can use and improve the features of a parent class.” The child class automatically gets the data members and methods of the parent class, and it may also add new members or override existing behavior.

Example idea:

Base Class:  Vehicle
   |
   +---- Derived Class: Car
   |
   +---- Derived Class: Bike

Here, Car and Bike inherit common features from Vehicle, such as speed, color, or methods like start() and stop().


Main Content

1. First Concept

What inheritance means in object-oriented programming

  • Inheritance is a relationship between classes where one class is built from another class.
  • The base class contains common characteristics, while the derived class contains specialized characteristics.
  • This relationship is often described as an “is-a” relationship.
    • Example: A Car is a Vehicle
    • Example: A Dog is an Animal
  • Inheritance helps organize programs into logical hierarchies, making them easier to understand and maintain.

How inherited members work

  • The derived class receives the accessible members of the base class.
  • In many programming languages, private members are not directly accessible in the child class, while protected and public members may be inherited depending on language rules.
  • Inherited methods can be used directly in the derived class, unless they are overridden.
  • The child class can also add its own new fields and methods, increasing functionality without repeating the parent’s code.

Example:

class Animal {
    void eat() {
        System.out.println("Animal eats food");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

In this example:

  • Dog inherits the eat() method from Animal
  • Dog also has its own method bark()

This means the object of Dog can use both inherited and newly defined behaviors.


2. Second Concept

Types and forms of inheritance

  • Single inheritance: one derived class inherits from one base class.
    • Example: Car inherits from Vehicle
  • Multilevel inheritance: a class inherits from a derived class, creating a chain.
    • Example: Vehicle -> Car -> ElectricCar
  • Hierarchical inheritance: multiple derived classes inherit from the same base class.
    • Example: Vehicle -> Car, Vehicle -> Bike
  • Multiple inheritance: one class inherits from more than one base class.
    • Common in languages like C++ and Python; not directly supported in Java for classes, though Java supports multiple inheritance through interfaces.
  • Hybrid inheritance: combination of two or more inheritance types.
    • Often achieved using interfaces or complex class relationships depending on the language.

ASCII view of common inheritance forms:

Single:
Parent
  |
 Child

Multilevel:
Grandparent
    |
 Parent
    |
 Child

Hierarchical:
      Parent
     /      \
 Child1    Child2

Why different inheritance forms matter

  • They help model real-world systems accurately.
  • They allow designers to represent general-to-specific relationships.
  • They improve code organization by avoiding duplication of shared features.
  • They influence how constructors, method access, and method resolution behave in a language.

Example of hierarchical inheritance:

class Shape {
    void draw() {
        System.out.println("Drawing a shape");
    }
}

class Circle extends Shape {
    void area() {
        System.out.println("Area of circle");
    }
}

class Square extends Shape {
    void perimeter() {
        System.out.println("Perimeter of square");
    }
}

Here, both Circle and Square inherit the shared draw() method from Shape.


3. Third Concept

Method overriding and polymorphism

  • Inheritance often leads to method overriding, where a derived class provides its own version of a method already defined in the base class.
  • Overriding allows specialized behavior while keeping the same method name and signature.
  • This is a major foundation of polymorphism, where the same method call behaves differently depending on the object type.
  • Example:
    • Animal.sound() may be generic
    • Dog.sound() may print "Bark"
    • Cat.sound() may print "Meow"

Example:

class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Meow");
    }
}

Constructors, super keyword, and inheritance

  • When a derived class object is created, the constructor of the base class is usually called first.
  • This ensures the parent part of the object is initialized before the child part.
  • Many languages provide a special keyword such as super to access the parent class constructor or parent class methods.
  • super can also be used to call a hidden parent method or refer to parent-class variables when there is name conflict.

Example:

class Vehicle {
    Vehicle() {
        System.out.println("Vehicle created");
    }
}

class Car extends Vehicle {
    Car() {
        super();
        System.out.println("Car created");
    }
}

Output:

Vehicle created
Car created

Why overriding and constructor chaining are important

  • They allow base functionality to be reused while still permitting customization.
  • They make object creation safe and structured.
  • They support dynamic behavior in large programs.
  • They are essential in frameworks, libraries, and real-world applications where behavior must vary by subclass.

Working / Process

1. Create the base class

  • Define the common properties and methods that multiple related classes will share.
  • Identify the features that are general enough to belong in the parent class.
  • Example: Vehicle may contain speed, fuel, start(), and stop().

2. Create the derived class using inheritance syntax

  • Specify that the child class extends or inherits from the parent class.
  • The child class automatically gets access to the inherited members according to access rules.
  • Example: class Car extends Vehicle
  • At this stage, the derived class can use inherited methods directly.

3. Extend, specialize, and use the inherited behavior

  • Add new fields and methods in the child class for specialized needs.
  • Override inherited methods when the default parent behavior is not sufficient.
  • Create objects of the derived class and use both inherited and own methods.
  • Example:
    • Car inherits start() from Vehicle
    • Car also defines openBoot()
    • Car may override fuelType() to return "Petrol" or "Electric"

Example process in code:

class Employee {
    String name;

    void work() {
        System.out.println("Employee is working");
    }
}

class Manager extends Employee {
    void conductMeeting() {
        System.out.println("Manager is conducting a meeting");
    }
}

Usage:

Manager m = new Manager();
m.name = "Asha";
m.work();
m.conductMeeting();

This shows:

  • Manager uses inherited member name
  • Manager uses inherited method work()
  • Manager adds its own method conductMeeting()

Advantages / Applications

Code reusability

  • Common code is written once in the base class and reused by all child classes.
  • This reduces duplication and saves development time.
  • If a common behavior changes, it can often be updated in one place.

Better organization and maintainability

  • Programs become easier to structure using parent-child class hierarchies.
  • Related features are grouped logically.
  • Maintenance becomes simpler because shared behavior is centralized.

Real-world modeling and extensibility

  • Inheritance is ideal for representing real-life relationships such as:
    • Person -> Student
    • Employee -> Manager
    • Shape -> Circle, Rectangle
  • It allows new specialized classes to be added with minimal code changes.
  • It supports extensible software design, especially in large systems and frameworks.

Common applications include:

  • Designing user interfaces with shared components
  • Building game characters and enemy hierarchies
  • Creating employee, account, product, or vehicle systems
  • Implementing reusable class libraries and frameworks

Summary

  • Inheritance lets one class acquire and extend the features of another class.
  • It supports reuse, specialization, and hierarchical design in object-oriented programming.
  • Important terms to remember: base class, derived class, superclass, subclass, overriding, super, is-a relationship