codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Inheritance Types (Single, Multiple)

Updated 2026-04-26
2 min read

Inheritance Types (Single, Multiple)

Inheritance is the mechanism in OOP by which one class (the child or subclass) acquires the properties and methods of another class (the parent or superclass). It represents an "IS-A" relationship.

A Dog IS-A Animal. A Car IS-A Vehicle. The child class inherits everything from the parent and can add its own unique features on top.

1. Why Inheritance?

Without inheritance, if you need a Car, Truck, and Motorcycle class that all share common attributes (speed, fuelLevel) and methods (accelerate(), brake()), you would copy-paste the same code three times. If a bug is found in accelerate(), you must fix it in three places.

With inheritance, you define the shared code once in a Vehicle parent class, and all three child classes inherit it automatically. Fix the bug once, and it's fixed everywhere.

class Vehicle {
    int speed;
    void accelerate() { speed += 10; }
    void brake() { speed -= 10; }
}

class Car extends Vehicle {
    int numDoors; // Car-specific attribute
    void openTrunk() { /* Car-specific behavior */ }
}

class Motorcycle extends Vehicle {
    boolean hasSidecar; // Motorcycle-specific attribute
}

2. Types of Inheritance

1. Single Inheritance

A child class inherits from exactly one parent class. This is the most common and simplest form.

  • Car extends Vehicle

2. Multilevel Inheritance

A child class inherits from a parent, which itself inherits from a grandparent. This forms a chain.

  • SportsCar extends Car extends Vehicle
class Vehicle { void start() {} }
class Car extends Vehicle { void openTrunk() {} }
class SportsCar extends Car { void activateTurbo() {} }
// SportsCar has access to start(), openTrunk(), AND activateTurbo()

3. Hierarchical Inheritance

Multiple child classes inherit from a single parent class. This forms a tree structure.

  • Car extends Vehicle, Truck extends Vehicle, Motorcycle extends Vehicle

4. Multiple Inheritance

A child class inherits from two or more parent classes simultaneously.

  • FlyingCar extends Car, Airplane

The Diamond Problem

Multiple inheritance creates a critical ambiguity called the Diamond Problem. If both Car and Airplane have a method called move(), which version does FlyingCar inherit?

Because of this ambiguity:

  • C++ allows multiple inheritance but forces the programmer to resolve the ambiguity manually using scope resolution.
  • Java completely bans multiple inheritance of classes. Instead, Java uses Interfaces to achieve a similar result safely (covered in a later chapter).

5. Hybrid Inheritance

A combination of two or more types of inheritance. For example, combining hierarchical and multiple inheritance. Like multiple inheritance, it can lead to the Diamond Problem and is generally avoided in Java.

3. The super Keyword

The super keyword in Java is a reference variable used to refer to the immediate parent class object.

class Animal {
    String name;
    Animal(String name) { this.name = name; }
    void speak() { System.out.println("..."); }
}

class Dog extends Animal {
    Dog(String name) {
        super(name); // Calls the parent's constructor
    }

    void speak() {
        super.speak(); // Calls the parent's speak() method
        System.out.println("Woof!");
    }
}


PreviousData AbstractionNextCompile-Time Polymorphism (Overloading)

Recommended Gear

Data AbstractionCompile-Time Polymorphism (Overloading)