In Java, inheritance is a mechanism that allows one class to inherit the
properties (fields and methods) of another class. There are several types
of inheritance in Java, each with its own characteristics. Below are the
types of inheritance, along with diagrams and example code.
1. Single Inheritance
A subclass inherits from a single superclass.
Diagram:
Superclass
|
Subclass
Code:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Defined in Dog
}
}
2. Multilevel Inheritance
A subclass inherits from a superclass, and another class inherits
from that subclass.
Diagram:
Superclass
|
Subclass
|
Sub-Subclass
Code:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is weeping.");
}
}
public class Main {
public static void main(String[] args) {
Puppy puppy = new Puppy();
puppy.eat(); // Inherited from Animal
puppy.bark(); // Inherited from Dog
puppy.weep(); // Defined in Puppy
}
}
3. Hierarchical Inheritance
Multiple subclasses inherit from a single superclass.
Diagram:
Superclass
/ | \
Subclass1 Subclass2 Subclass3
Code:
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Defined in Dog
Cat cat = new Cat();
cat.eat(); // Inherited from Animal
cat.meow(); // Defined in Cat
}
}
4. Multiple Inheritance (Not Supported in Java)
A subclass inherits from multiple superclasses.
Note: Java does not support multiple inheritance with classes to
avoid complexity and ambiguity (e.g., the "diamond problem").
Diagram:
Copy
Superclass1 Superclass2
\ /
Subclass
Workaround: Use interfaces to achieve multiple inheritance.
Code:
java
Copy
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
System.out.println("Dog is eating.");
}
public void play() {
System.out.println("Dog is playing.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // From Animal interface
dog.play(); // From Pet interface
}
}
5. Hybrid Inheritance
A combination of two or more types of inheritance.
Note: Java does not directly support hybrid inheritance due to the
lack of multiple inheritance support.
Diagram:
Superclass
|
Subclass1
|
Subclass2
|
Subclass3
Code: (Combination of single and hierarchical inheritance)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is weeping.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing.");
}
}
public class Main {
public static void main(String[] args) {
Puppy puppy = new Puppy();
puppy.eat(); // Inherited from Animal
puppy.bark(); // Inherited from Dog
puppy.weep(); // Defined in Puppy
Cat cat = new Cat();
cat.eat(); // Inherited from Animal
cat.meow(); // Defined in Cat
}
}
Summary of Inheritance Types:
Supported in
Type Description
Java?
Single
Yes One subclass inherits from one superclass.
Inheritance
Multilevel Yes A chain of inheritance (e.g., A → B → C).
Multiple subclasses inherit from one
Hierarchical Yes
superclass.
Multiple No (with One subclass inherits from multiple
Supported in
Type Description
Java?
classes) superclasses (achieved via interfaces).
Hybrid No (directly) A combination of inheritance types.