Inheritance is a key feature of Object-Oriented Programming (OOP) that allows a class (called the subclass or child class) to inherit properties and behaviors from another class (called the superclass or parent class). Inheritance promotes code reuse, modularity, and hierarchical class structure.
Key Concepts of Inheritance
- Parent Class (Superclass):
- The class whose members are inherited.
- Defined using the standard class keyword.
- Child Class (Subclass):
- The class that inherits from the parent class.
- Uses the extends keyword to establish inheritance.
- Access Modifiers and Inheritance:
- Public and protected members of the parent class can be accessed by the child class.
- Private members are not directly accessible in the child class.
- super Keyword:
- Refers to the parent class.
- Can be used to access superclass methods, constructors, and variables.
Types of Inheritance in Java
- Single Inheritance:
- A class inherits from one parent class.
- Example:
class Animal {
void eat() {
System.out.println(“This animal eats food.”);
}
}
class Dog extends Animal {
void bark() {
System.out.println(“Dog barks.”);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark();
}
}
- Multilevel Inheritance:
- A class inherits from another class, which itself inherits from another.
- Example:
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(“Dog barks.”);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.walk();
dog.bark();
}
}
- Hierarchical Inheritance:
- Multiple classes inherit from a single parent class.
- Example:
class Animal {
void eat() {
System.out.println(“This animal eats food.”);
}
}
class Dog extends Animal {
void bark() {
System.out.println(“Dog barks.”);
}
}
class Cat extends Animal {
void meow() {
System.out.println(“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();
}
}
- Multiple Inheritance with Interfaces:
- Java does not support multiple inheritance with classes to avoid ambiguity.
- However, it is allowed using interfaces.
Advantages of Inheritance
- Code Reusability:
- Allows reuse of existing code, reducing redundancy.
- Improved Code Maintenance:
- Changes in the parent class automatically apply to child classes.
- Polymorphism:
- Enables dynamic method binding and overriding for flexible design.
- Extensibility:
- New functionalities can be added easily by extending existing classes.
Method Overriding
When a subclass provides a specific implementation of a method in the parent class:
- The method in the subclass must have the same name, return type, and parameters.
- Annotated with @Override for clarity.
Example:
class Animal {
void sound() {
System.out.println(“Animals make sounds.”);
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println(“Dog barks.”);
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound(); // Output: Dog barks.
}
}
Accessing Parent Class Members
- Using super Keyword:
- Access a parent class method or variable hidden by the subclass.
Example:
class Parent {
String name = “Parent”;
void display() {
System.out.println(“This is the parent class.”);
}
}
class Child extends Parent {
String name = “Child”;
void display() {
super.display(); // Call parent class method
System.out.println(“This is the child class.”);
System.out.println(“Parent name: ” + super.name);
System.out.println(“Child name: ” + name);
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.display();
}
}
Output:
This is the parent class.
This is the child class.
Parent name: Parent
Child name: Child
- Constructor Chaining:
- Use super() to call the parent class constructor from the child class constructor.
Example:
class Parent {
Parent() {
System.out.println(“Parent constructor.”);
}
}
class Child extends Parent {
Child() {
super(); // Calls parent class constructor
System.out.println(“Child constructor.”);
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
}
}
Output:
Parent constructor.
Child constructor.
Key Points to Remember
- Single Parent:
- A class can inherit from only one parent class directly.
- Private Members:
- Private members of the parent class are not inherited directly but can be accessed via public or protected methods.
- Constructors:
- Constructors are not inherited, but they are called during object creation of the child class.
- Final Classes and Methods:
- A final class cannot be extended.
- A final method cannot be overridden.
Disadvantages of Inheritance
- Tight Coupling:
- Changes in the parent class may affect the child classes.
- Overhead:
- Subclasses may inherit unnecessary members, increasing complexity.
- Can Lead to Misuse:
- Improper use of inheritance can lead to poorly designed code.
Conclusion
Inheritance is a powerful feature of Java that simplifies code reuse and promotes a hierarchical structure. It enables efficient code management and supports OOP principles like polymorphism and extensibility. However, it should be used carefully to avoid unnecessary dependencies or complexity.