Java Inheritance
Inheritance is one of the core principles of object-oriented programming (OOP) in Java. It allows one class to acquire the properties (fields) and behaviors (methods) of another class. This promotes code reuse, extensibility, and logical hierarchy in software design.
What Is Inheritance in Java?
Inheritance enables a class to reuse and extend functionality from an existing class.
Key terminology
- Superclass (Parent class) — the class whose members are inherited
- Subclass (Child class) — the class that inherits from another class
In Java, inheritance is implemented using the extends keyword.
Basic Inheritance Example
In this example, a Car class inherits common vehicle features from a Vehicle class.
class Vehicle {
protected String brand;
protected int maxSpeed;
public Vehicle(String brand, int maxSpeed) {
this.brand = brand;
this.maxSpeed = maxSpeed;
}
public void start() {
System.out.println(brand + " vehicle is starting...");
}
}
class Car extends Vehicle {
private String model;
public Car(String brand, int maxSpeed, String model) {
super(brand, maxSpeed); // Call superclass constructor
this.model = model;
}
public void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Max Speed: " + maxSpeed +"km/h")
}
public static void main(String[] args) {
Car car = new Car("Toyota", 220, "Camry");
car.start(); // inherited method
car.displayDetails(); // subclass method
}
}
Output
Toyota vehicle is starting...
Brand: Toyota
Model: Camry
Max Speed: 220 km/h
Understanding the protected Access Modifier
Notice that the brand and maxSpeed fields in Vehicle are declared as protected.
Why protected?
protected members are accessible:
- within the same class
- within the same package
- in subclasses (even in different packages)
If these fields were private, the subclass could not access them directly.
Why Use Inheritance in Java?
Inheritance is useful when classes share common characteristics.
Benefits
- Promotes code reuse
- Establishes is-a relationships (Car is a Vehicle)
- Simplifies maintenance
- Enables polymorphism
- Supports hierarchical classification
Real-World Example of Inheritance
class Employee {
protected String name;
protected double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void work() {
System.out.println(name + " is working.");
}
}
class Developer extends Employee {
private String programmingLanguage;
public Developer(String name, double salary, String language) {
super(name, salary);
this.programmingLanguage = language;
}
public void showProfile() {
System.out.println(name + " earns ₹" + salary +
" and codes in " + programmingLanguage);
}
public static void main(String[] args) {
Developer dev = new Developer("Arjun", 80000, "Java");
dev.work(); // inherited
dev.showProfile(); // subclass method
}
}
The final Keyword in Inheritance
If you want to prevent a class from being inherited, declare it as final.
final class BankAccount {
public void display() {
System.out.println("Bank account details");
}
}
// Not allowed
class SavingsAccount extends BankAccount {
}
Compiler error
error: cannot inherit from final BankAccount