Inheritance is an OOP mechanism in Java where one class (child/subclass) acquires the properties and behaviors of another class (parent/superclass).
It allows:
-
Reusability of code
-
Method overriding
-
Extending or customizing parent functionality
Basic syntax:
class Parent { }
class Child extends Parent { }The extends keyword establishes the parent–child relationship.
-
Code Reusability Common properties can be placed in a parent class.
-
Reduced Redundancy Avoids duplicating same methods/fields in multiple classes.
-
Method Overriding (Polymorphism) Child classes can implement their own version of the same method.
-
Extensibility New features can be added with minimal changes.
Java supports four types of inheritance:
| Type | Supported in Java? | Example |
|---|---|---|
| Single Inheritance | ✔ Supported | Child extends Parent |
| Multilevel Inheritance | ✔ Supported | GrandChild → Child → Parent |
| Hierarchical Inheritance | ✔ Supported | Multiple children extending one parent |
| Hybrid Inheritance | Partially | Achieved using interfaces |
| Multiple Inheritance | ❌ Not through classes | Allowed via interfaces |
class A {
void show() { System.out.println("A show"); }
}
class B extends A { }
public class Demo {
public static void main(String[] args) {
B obj = new B();
obj.show();
}
}class A { }
class B extends A { }
class C extends B { }class Animal { }
class Dog extends Animal { }
class Cat extends Animal { }Java avoids it to prevent diamond problem.
A
/ \
B C
\ /
D ← ambiguity (which A's method?)
But multiple inheritance is possible using interfaces.
interface A { void m1(); }
interface B { void m1(); }
class C implements A, B {
public void m1() { }
}Child Object (in Heap)
+----------------------+
| parent fields | ← inherited
| child fields |
| parent methods | ← inherited
| child methods |
+----------------------+
Memory flow:
Child obj = new Child();-
One single object is created in heap
-
It contains both parent and child members
-
objpoints to the entire combined structure
Constructor calls follow the top-down chain.
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child constructor");
}
}Output:
Parent constructor
Child constructor
- First
super()is called (implicitly if not written) - Then the child constructor executes
Inheritance enables overriding — child modifies parent method.
class Parent {
void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child show");
}
}Parent reference pointing to child object:
Parent p = new Child();
p.show(); // calls Child's show() → runtime polymorphismThis enables dynamic behavior and polymorphism.
Not all members are inherited.
| Modifier | Inherited? | Accessible in Child? | Notes |
|---|---|---|---|
public |
✔ | ✔ | Fully accessible |
protected |
✔ | ✔ | Accessible within same package or via subclass |
default |
✔ | ✔ (only if in same package) | Package-private |
private |
❌ | ❌ | Not inherited |
Private members are not inherited, but are part of object memory.
super is used to:
- Access parent class variables
- Access parent class methods
- Call parent class constructor
Example:
class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void display() {
System.out.println(super.x); // parent variable
System.out.println(this.x); // child variable
}
}final class A { }
class B extends A { } // error ❌class A {
final void show() { }
}
class B extends A {
void show() { } // error ❌
}class Vehicle {
int speed;
void move() {
System.out.println("Vehicle is moving");
}
}class Car extends Vehicle {
String brand;
@Override
void move() {
System.out.println(brand + " car is moving");
}
}Car c = new Car();
c.brand = "Tesla";
c.move();Output:
Tesla car is moving
To avoid ambiguity (diamond problem).
No, but child class constructors call parent constructors using super().
They exist in the memory of the child object but are not accessible.
When child object is assigned to parent reference, child-specific members cannot be accessed.
- Inheritance = “is-a” relationship
- Composition = “has-a” relationship
-
Inheritance allows a child class to extend a parent class.
-
Promotes code reusability and runtime polymorphism.
-
Constructors are executed from parent to child.
-
Private members are not accessible but remain part of object.
-
Multiple inheritance is restricted to avoid ambiguity but supported through interfaces.