What is Inheritance in Java?
Inheritance is one of the core principles of Object-Oriented Programming (OOP) in Java. It allows one class (child class) to acquire properties and behaviors (methods) from another class (parent class). This concept promotes code reuse, makes the program more modular and simplifies maintenance.
In Java, inheritance is implemented using the extends keyword.
Why Use Inheritance?
- Code Reusability: Avoid rewriting the same code for related classes.
- Extensibility: Easily add new features or behaviors to child classes.
- Simplified Maintenance: Modify common behaviors in one place (parent class).
- Polymorphism: Leverage the ability to override methods to provide specific functionality.
How Inheritance Works
Inheritance enables a child class to access:
- Public Members: Accessible directly.
- Protected Members: Accessible within the same package or through inheritance.
- Default Members: Accessible within the same package.
- Private Members: Not directly accessible by child classes but accessible using getters and setters.
Types of Inheritance in Java
Java supports the following types of inheritance:
- Single Inheritance: One child class inherits from one parent class.
- Multilevel Inheritance: A child class inherits from a parent class, and another class inherits from the child class.
- Hierarchical Inheritance: Multiple child classes inherit from the same parent class.
Note: Java does not support multiple inheritance with classes to avoid ambiguity caused by the “Diamond Problem.” However, it can be achieved using interfaces.
Syntax of Inheritance
class Parent {
void display() {
System.out.println("This is the parent class.");
}
}
class Child extends Parent {
void show() {
System.out.println("This is the child class.");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.display(); // Inherited from Parent class
obj.show(); // Method of Child class
}
}
Output:
This is the parent class.
This is the child class.
Examples of Java Inheritance
1. Single Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Specific to Dog
}
}
Output:
This animal eats food.
The dog barks.
2. Multilevel Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("This mammal walks.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // From Animal class
dog.walk(); // From Mammal class
dog.bark(); // From Dog class
}
}
Output:
This animal eats food.
This mammal walks.
The dog barks.
3. Hierarchical Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
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();
dog.bark();
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
Output:
This animal eats food.
The dog barks.
This animal eats food.
The cat meows.
Overriding in Inheritance
Method overriding allows a child class to provide a specific implementation for a method already defined in the parent class. The method in the child class must have the same name, return type, and parameters.
Example of Overriding:
class Parent {
void show() {
System.out.println("This is the parent class.");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("This is the child class.");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child();
obj.show(); // Calls the overridden method in Child
}
}
Output:
This is the child class.
Final Keyword in Inheritance
Final Class: A class declared as final cannot be inherited.
final class Parent {
// Code here
}
class Child extends Parent { // This will cause an error
}
Final Method: A final method cannot be overridden by a child class.
class Parent {
final void display() {
System.out.println("This is a final method.");
}
}
class Child extends Parent {
void display() { // This will cause an error
}
}
Advantages of Inheritance
- Reduces redundancy by reusing code.
- Enhances code readability and organization.
- Simplifies testing and debugging.
- Allows for polymorphism.
Disadvantages of Inheritance
- Increases dependency between parent and child classes.
- Changes in the parent class can affect child classes.
- Not suitable for all relationships, especially unrelated objects.