← Back to Home

Inheritance

Inheritance is an object-oriented programming (OOP) concept where one class (child/subclass) acquires the properties and behaviors of another class (parent/superclass). It promotes code reuse, extensibility, and hierarchical design.

This is a high-frequency interview topic and foundational for understanding polymorphism and method overriding.

What Is Inheritance?

  • One class extends another class
  • Child class inherits fields and methods
  • Enables IS-A relationship
  • Improves code reuse and maintainability
class Parent {
    int x = 10;
}

class Child extends Parent {
    int y = 20;
}
          

Here: Child IS-A Parent

Why Inheritance Is Important

  • Avoids code duplication
  • Supports extensibility
  • Enables polymorphism
  • Improves maintainability
  • Represents real-world hierarchies

Basic Syntax

class SubClass extends SuperClass {
    // additional members
}
          

Example: Simple Inheritance

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

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

public class Test {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();   // inherited
        d.bark();  // own method
    }
}
          

Types of Inheritance in Java

1. Single Inheritance

One child inherits from one parent.

class B extends A { }
          

✔ Supported

2. Multilevel Inheritance

Chain of inheritance.

class A { }
class B extends A { }
class C extends B { }
          

✔ Supported

3. Hierarchical Inheritance

Multiple children inherit from one parent.

class A { }
class B extends A { }
class C extends A { }
          

✔ Supported

4. Multiple Inheritance (Classes)

class C extends A, B { } // ❌ Not allowed
          

❌ Not supported using classes

✔ Achieved using interfaces

extends Keyword

  • Used to inherit a class
  • Only one class can be extended
  • Establishes parent-child relationship

What Is Inherited and What Is Not

Inherited:

  • Public members
  • Protected members
  • Default members (same package)

Not Inherited:

  • Private members
  • Constructors

Constructor Behavior in Inheritance

class A {
    A() {
        System.out.println("A constructor");
    }
}

class B extends A {
    B() {
        System.out.println("B constructor");
    }
}
          

Output:

A constructor
B constructor
          
  • Parent constructor executes first
  • super() is called implicitly

super Keyword (Preview)

  • Access parent class members
  • Call parent constructor
super.methodName();
super();
          

Method Overriding (Preview)

Child class provides specific implementation of parent method.

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

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

✔ Runtime polymorphism

Inheritance vs Composition (Design Insight)

Aspect Inheritance Composition
Relationship IS-A HAS-A
Coupling Tight Loose
Flexibility Less More

Best Practice: Prefer composition unless true IS-A relationship exists.

Common Beginner Mistakes

  • Using inheritance where IS-A does not apply
  • Accessing private members directly
  • Forgetting parent constructor call
  • Overusing inheritance
  • Confusing inheritance with interfaces

Interview-Ready Answers

Short Answer

Inheritance allows one class to acquire the properties and behaviors of another class.

Detailed Answer

In Java, inheritance is achieved using the extends keyword, allowing a subclass to reuse fields and methods of a superclass. Java supports single, multilevel, and hierarchical inheritance, but not multiple inheritance using classes. Inheritance enables code reuse, extensibility, and polymorphism.

Key Takeaway

Inheritance builds hierarchical relationships and enables code reuse, but it should be used judiciously to avoid tight coupling and design rigidity.