Inheritance
Inheritance is one of the core principles of object-oriented programming (OOP) in Java. It allows a class (called the child or subclass) to inherit properties and behaviors (fields and methods) from another class (called the parent or superclass). Inheritance promotes code reuse, modularity, and extensibility, making it easier to build and maintain complex applications.
What is Inheritance?
Inheritance is a mechanism in Java where one class acquires the properties (fields) and behaviors (methods) of another class. The class that inherits is called the subclass (child class), and the class being inherited from is called the superclass (parent class).
Key Features of Inheritance:
- Code Reusability: Common functionality can be defined in the parent class and reused in child classes.
- Method Overriding: Subclasses can provide specific implementations for methods defined in the parent class.
- IS-A Relationship: Inheritance represents an "is-a" relationship. For example, a Dog "is-a" type of Animal.
Syntax of Inheritance
Inheritance in Java is implemented using the extends keyword.
class ParentClass {
// Fields and methods
}
class ChildClass extends ParentClass {
// Additional fields and methods
}
Example
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(); // Defined in Dog
}
}
Types of Inheritance in Java
Java supports the following types of inheritance:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
1. Single Inheritance
In single inheritance, a subclass inherits from a single parent class.
class Vehicle {
void start() {
System.out.println("Vehicle is starting.");
}}
class Car extends Vehicle {
void drive() {
System.out.println("Car is driving.");
}}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start(); // Inherited from Vehicle
car.drive(); // Defined in Car
}}
2. Multilevel Inheritance
In multilevel inheritance, a class inherits from a class that is already a subclass of another class.
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(); // Inherited from Animal
dog.walk(); // Inherited from Mammal
dog.bark(); // Defined in Dog
}
}
3. Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from a single parent class.
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(); // Inherited from Animal
dog.bark(); // Defined in Dog
Cat cat = new Cat();
cat.eat(); // Inherited from Animal
cat.meow(); // Defined in Cat
}
}
Why Java Does Not Support Multiple Inheritance?
Java does not support multiple inheritance with classes to avoid ambiguity caused by the Diamond Problem. The diamond problem occurs when a class inherits from two classes that have methods with the same name, leading to confusion about which method to execute.
Instead, Java provides interfaces to achieve multiple inheritance-like behavior. A class can implement multiple interfaces.
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
Method Overriding in Inheritance
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in the parent class. The overridden method in the subclass must have the same name, return type, and parameters as the method in the parent class.
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound(); // Output: The dog barks.
}
}
The super Keyword
The super keyword is used in inheritance to refer to the parent class. It can be used to:
- Access parent class methods or variables.
- Call the parent class constructor.
Example: Accessing Parent Class Methods
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
}
}
class Dog extends Animal {
void sound() {
super.sound(); // Call parent class method
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Example: Calling Parent Class Constructor
class Animal {
Animal() {
System.out.println("Animal constructor called.");
}
}
class Dog extends Animal {
Dog() {
super(); // Call parent class constructor
System.out.println("Dog constructor called.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
}
}
Advantages of Inheritance
- Code Reusability: Common functionality can be written once in the parent class and reused in child classes.
- Extensibility: New functionality can be added to existing classes without modifying them.
- Polymorphism: Inheritance enables polymorphism, allowing a subclass to override methods and provide specific behavior.
- Improved Code Organization: Related classes can be grouped together in a hierarchy, making the codebase easier to understand and maintain.
Limitations of Inheritance
- Tight Coupling: Subclasses are tightly coupled to their parent classes, making changes in the parent class potentially affect all subclasses.
- Increased Complexity: Overusing inheritance can lead to complex and hard-to-maintain hierarchies.
- Not Always Suitable: Inheritance should only be used when there is a clear "is-a" relationship. Otherwise, composition or interfaces may be more appropriate.
