Inheritance is one of the core principles of Object-Oriented Programming (OOP). It allows a class (subclass or child class or derived class) to acquire the properties and behaviors of another class (superclass or parent class or base class). This promotes code reuse, scalability, and maintainability.
Inheritance is a mechanism where a child class derives properties and behaviors from a parent class. The child class can:
- Use the fields and methods of the parent class
- Override parent class methods to provide a specific implementation
- Add its own additional properties and methods
- Code Reusability: Avoids code duplication by reusing fields and methods of the parent class.
- Improves Maintainability: Reduces redundancy, making code easier to manage.
- Enhances Extensibility: New functionality can be added easily without modifying existing code.
The parent class contains common fields and methods.
// Parent class
public class Animal {
String name;
void eat() {
System.out.println(name + " is eating...");
}
}The child class inherits the properties and methods of the parent class.
// Child class
public class Dog extends Animal {
void bark() {
System.out.println(name + " is barking...");
}
}Now, let's create an object and use the inherited methods.
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.eat(); // Inherited from Animal class
myDog.bark(); // Defined in Dog class
}
}Buddy is eating...
Buddy is barking...
Java supports different types of inheritance:
A subclass inherits from one superclass.
class Parent {
void show() {
System.out.println("This is the parent class");
}
}
class Child extends Parent {
void display() {
System.out.println("This is the child class");
}
}A subclass inherits from another subclass, forming a chain.
class Grandparent {
void show() {
System.out.println("Grandparent class");
}
}
class Parent extends Grandparent {
void display() {
System.out.println("Parent class");
}
}
class Child extends Parent {
void print() {
System.out.println("Child class");
}
}A single parent class has multiple child classes.
class Parent {
void show() {
System.out.println("Parent class");
}
}
class Child1 extends Parent {
void display() {
System.out.println("Child1 class");
}
}
class Child2 extends Parent {
void print() {
System.out.println("Child2 class");
}
}Note: Java does not support multiple inheritance (i.e., a child class inheriting from multiple parents) due to ambiguity problems.
Method overriding allows a child class to redefine a method from the parent class.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog(); // Polymorphism
myAnimal.makeSound();
}
}Dog barks
The super keyword is used to refer to the parent class. It helps to:
- Call the parent class constructor.
- Access the parent class methods.
- Access the parent class fields.
class Animal {
Animal() {
System.out.println("Animal Constructor");
}
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the parent class constructor
System.out.println("Dog Constructor");
}
void makeSound() {
super.makeSound(); // Calls parent method
System.out.println("Dog barks");
}
}public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
}
}Animal Constructor
Dog Constructor
Animal makes a sound
Dog barks