Inheritance in Java
Inheritance in Java
Introduction
Inheritance is one of the four core concepts of Object-Oriented Programming (OOP) in Java. It allows a class to inherit properties and behaviors (fields and methods) from another class, promoting code reusability, scalability, and modularity.
In this article, we'll explore:
What inheritance is
Types of inheritance in Java
Syntax and implementation
Real-world examples
Best practices and rules
Inheritance is the mechanism where one class (called the subclass or child class) derives the properties and behaviors of another class (called the superclass or parent class).
Java uses the extends
keyword to implement inheritance:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
Dog d = new Dog();
d.eat(); // Inherited method
One subclass inherits from one superclass.
class A {}
class B extends A {}
A class inherits from a class which in turn inherits from another class.
class A {}
class B extends A {}
class C extends B {}
Multiple subclasses inherit from a single superclass.
class A {}
class B extends A {}
class C extends A {}
Java does not support multiple inheritance (a class inheriting from more than one class) with classes to avoid ambiguity. However, it supports multiple inheritance with interfaces.
The class whose features are inherited.
The class that inherits the features of another class.
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle {
void speed() {
System.out.println("Bike speed is 80 km/hr");
}
}
class Employee {
String name;
double salary;
void displayInfo() {
System.out.println("Name: " + name + ", Salary: " + salary);
}
}
class Manager extends Employee {
String department;
void displayDept() {
System.out.println("Department: " + department);
}
}
public class Main {
public static void main(String[] args) {
Manager m = new Manager();
m.name = "Alice";
m.salary = 75000;
m.department = "IT";
m.displayInfo();
m.displayDept();
}
}
A subclass can provide its own implementation of a method already defined in the parent class.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
Promotes code reuse
Simplifies code maintenance
Enables method overriding and polymorphism
Improves readability and structure
Use inheritance only when classes are logically related
Favor composition over inheritance if reuse isn't structural
Avoid deep inheritance chains (more than 2-3 levels)
Use super
keyword to access superclass methods and constructors
Q1: Can a class extend more than one class in Java?
No. Java does not support multiple inheritance with classes.
Q2: What is the use of super
keyword?
It refers to the parent class and can be used to access parent class members and constructors.
Q3: Can constructors be inherited?
No, but a subclass can call the constructor of its superclass using super()
.