Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP). It allows a new class (known as a subclass or child class) to inherit properties (fields) and behaviors (methods) from an existing class (known as a superclass or parent class).
In Java, inheritance is implemented using the extends keyword, where a subclass inherits the public and protected members (fields and methods) of its superclass, and can also add its own members or override inherited methods.
Benefits of Inheritance
1. Code Reusability: Reuse code from the parent class in the subclass, avoiding redundancy.
2. Extensibility: A subclass can extend and modify the functionality of a parent class.
3. Hierarchical Structure: Inheritance allows you to build hierarchical relationships between classes (e.g., Animal → Dog → Beagle).
Types of Inheritance in Java
Single Inheritance: A class inherits from one superclass.
Multilevel Inheritance: A class inherits from another class, which itself is a subclass of another class.
Hierarchical Inheritance: Multiple classes inherit from a single superclass.
Hybrid Inheritance: A combination of more than one type of inheritance (not supported directly in Java due to ambiguity in multiple inheritance).
Single Inheritance
In single inheritance, a subclass inherits from a single superclass. This is the most basic form of inheritance, where one class inherits the properties and methods of another.
In single inheritance, a child class inherits from only one parent class.
Example:
class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
public 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(); // Inherited from Dog
}
}
Output:
Dog is barking
Multilevel Inheritance
In multilevel inheritance, a class is derived from another class, which is itself derived from another class. Essentially, this forms a chain of inheritance.
Multilevel inheritance is when a class inherits from a class that is already a subclass of another class.
Example:
class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking");
}
}
class PitBull extends Dog {
public void quality() {
System.out.println("Pit Bull is heighly intelligent");
}
}
public class Main {
public static void main(String[] args) {
PitBull pibull = new PitBull();
pibull.eat(); // Inherited from Animal
pibull.bark(); // Inherited from Dog
pibull.quality(); // Inherited from PitBull
}
}
Output:
Dog is barking
Pit Bull is heighly intelligent
Explanation:
PitBull inherits from Dog.
Dog inherits from Animal. So, PitBull indirectly inherits from Animal through Dog.
Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from a single superclass. This type of inheritance is common when different classes share common properties or methods but also have unique behaviors.
In hierarchical inheritance, one parent class is extended by multiple subclasses.
Example:
// Parent class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Child class 1
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
// Child class 2
class Cat extends Animal {
void meow() {
System.out.println("The cat meows.");
}
}
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
}
}
Output:
The dog barks.
This animal eats food.
The cat meows.
Explanation: In this example, both Dog and Cat inherit the eat() method from the Animal class.
Multiple Inheritance
Java does not support multiple inheritance directly through classes, which means you cannot inherit from more than one class. This is done to avoid ambiguity and complexity (the “diamond problem”). However, multiple inheritance can be achieved using interfaces.
Multiple inheritance occurs when a class inherits from more than one class. In Java, this is possible using interfaces, where a class can implement multiple interfaces.
Example:
// First interface
interface Eatable {
void eat();
}
// Second interface
interface Runnable {
void run();
}
// Child class implementing both interfaces
class Dog implements Eatable, Runnable {
public void eat() {
System.out.println("The dog eats food.");
}
public void run() {
System.out.println("The dog runs fast.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Implemented from Eatable interface
dog.run(); // Implemented from Runnable interface
}
}
Output:
The dog runs fast.
Explanation:
- Dog implements both Eatable and Runnable interfaces.
- This allows the Dog class to inherit behavior from multiple sources, providing a form of multiple inheritance.
Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance, typically combining interfaces and classes. It is not directly supported by Java through classes alone but can be implemented using interfaces.
Hybrid inheritance is a combination of two or more types of inheritance. It typically involves using both classes and interfaces.
Example:
class Animal {
void live() {
System.out.println("Animals live in jungle.");
}
}
// First interface
interface Eatable {
void eat();
}
// Second interface
interface Runnable {
void run();
}
// Child class implementing both interfaces
class Dog extends Animal implements Eatable, Runnable {
public void eat() {
System.out.println("The dog eats food.");
}
public void run() {
System.out.println("The dog runs fast.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.live(); // inherited from Animal class
dog.eat(); // Implemented from Eatable interface
dog.run(); // Implemented from Runnable interface
}
}
Output:
The dog eats food.
The dog runs fast.
Explanation:
In this example, Dog class inherits from Animal (single inheritance) and implements two interfaces (Eatable and Runnable), which shows hybrid inheritance.