1. What is Inheritance in Java?
Inheritance is an Object-Oriented Programming (OOP) concept where a class (child/subclass) acquires the properties and behaviors (fields and methods) of another class (parent/superclass).
Purpose:
- Reusability: Avoids rewriting code.
- Extensibility: Add new features easily.
- Polymorphism Support: Enables overriding methods.
Syntax:
class Parent {
int a = 10;
void display() {
System.out.println("Parent method");
}
}
class Child extends Parent {
void show() {
System.out.println("Child method");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.display(); // inherited from Parent
c.show(); // Child's own method
}
}
2. Types of Inheritance in Java
Java supports multiple types of inheritance except multiple inheritance using classes (to avoid ambiguity, e.g., Diamond problem).
A. Single Inheritance
- Definition: A child class inherits from one parent class.
- Example:
class Vehicle {
void start() { System.out.println("Vehicle started"); }
}
class Car extends Vehicle {
void honk() { System.out.println("Car honks"); }
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.start();
c.honk();
}
}
- Real-time Example: Car is a Vehicle. Car inherits features of Vehicle (start engine, fuel system).
Diagram:
Vehicle
|
V
Car
B. Multilevel Inheritance
- Definition: A class inherits from a class, which in turn inherits from another class.
- Example:
class Animal {
void eat() { System.out.println("Animal eats"); }
}
class Mammal extends Animal {
void walk() { System.out.println("Mammal walks"); }
}
class Dog extends Mammal {
void bark() { System.out.println("Dog barks"); }
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.walk();
d.bark();
}
}
- Real-time Example: Dog is a Mammal, Mammal is an Animal. So Dog inherits features of Mammal and Animal.
Diagram:
Animal
|
Mammal
|
Dog
C. Hierarchical Inheritance
- Definition: Multiple classes inherit from one parent class.
- Example:
class Vehicle {
void start() { System.out.println("Vehicle starts"); }
}
class Car extends Vehicle {
void honk() { System.out.println("Car honks"); }
}
class Bike extends Vehicle {
void kick() { System.out.println("Bike kicks"); }
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.start();
c.honk();
Bike b = new Bike();
b.start();
b.kick();
}
}
- Real-time Example: Car and Bike are Vehicles. Both inherit Vehicle’s start() feature.
Diagram:
Vehicle
/ \
Car Bike
D. Multiple Inheritance (Through Interfaces)
- Note: Java does not allow multiple inheritance with classes to avoid ambiguity (diamond problem). But we can achieve it using interfaces.
- Example:
interface Engine {
void engineStart();
}
interface Fuel {
void fuelType();
}
class Car implements Engine, Fuel {
public void engineStart() { System.out.println("Engine starts"); }
public void fuelType() { System.out.println("Uses petrol"); }
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.engineStart();
c.fuelType();
}
}
- Real-time Example: Car can have features of both Engine and Fuel.
Diagram:
Engine Fuel
\ /
Car
E. Hybrid Inheritance
- Definition: Combination of two or more inheritance types. Java implements it via classes + interfaces.
- Example:
interface A {
void methodA();
}
class B {
void methodB() { System.out.println("Method B"); }
}
class C extends B implements A {
public void methodA() { System.out.println("Method A"); }
}
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
- Real-time Example: C class inherits B (single) and A (interface), combining behaviors.
Diagram:
A(interface) B(class)
\ /
C
3. Key Points / Disclosures
- Java supports: Single, Multilevel, Hierarchical, Hybrid (with interfaces), Multiple (via interfaces).
- Java does not support: Multiple inheritance with classes.
super
keyword: Refers to parent class object. Can call parent constructor or method.- Constructor chaining: Parent class constructor is called first.
- Access Modifiers Impact: Private members of parent class cannot be inherited, but protected and public can be.
Example of super
:
class Parent {
Parent() { System.out.println("Parent constructor"); }
}
class Child extends Parent {
Child() {
super(); // Calls Parent constructor
System.out.println("Child constructor");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
}
}
Output:
Parent constructor
Child constructor
✅ Summary Table:
Type | Example | Real-time Use Case |
---|---|---|
Single | Car extends Vehicle | Car is a Vehicle |
Multilevel | Dog -> Mammal -> Animal | Dog inherits Mammal & Animal features |
Hierarchical | Car, Bike -> Vehicle | Car & Bike share Vehicle features |
Multiple (interface) | Car implements Engine,Fuel | Car uses Engine & Fuel features |
Hybrid | C extends B implements A | Combines class + interface features |