Skip to main content

Inheritance in Java

  • Inheritance is a mechanism in Java where a new class, known as the subclass (or derived class), is derived from an existing class, known as the superclass (or base class).

  • The subclass inherits the fields and methods of the superclass, allowing it to reuse code and enhance or modify its behavior.

  • Inheritance is a fundamental principle of Object-Oriented Programming (OOP) that allows a class to inherit properties and behaviors from another class.

  • This enables code reuse and establishes a natural hierarchy between classes.

Key Characteristics of Inheritance​

  • Reusability: Inheritance promotes code reusability by allowing a new class to reuse methods and fields of an existing class.

  • Hierarchy: It establishes a parent-child relationship between the superclass and the subclass.

  • Extensibility: The subclass can extend the functionalities of the superclass by adding new fields and methods or overriding existing ones.

Syntax of Inheritance​

In Java, inheritance is achieved using the extends keyword.

class Superclass {
// Fields and methods of the superclass
}

class Subclass extends Superclass {
// Fields and methods of the subclass
}

Example of Inheritance​

Let’s look at an example to understand how inheritance works.

Defining the Superclass​

 class Vehicle {
// Instance variables
String brand;
int wheels;

// Constructor
public Vehicle(String brand, int wheels) {
this.brand = brand;
this.wheels = wheels;
}

// Instance method
public void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Number of Wheels: " + wheels);
}
}

Defining the Subclass​

 class Car extends Vehicle {
// Additional instance variable
int doors;

// Constructor
public Car(String brand, int wheels, int doors) {
super(brand, wheels); // Call to superclass constructor
this.doors = doors;
}

// Overriding method
@Override
public void displayDetails() {
super.displayDetails(); // Call to superclass method
System.out.println("Number of Doors: " + doors);
}
}

Using the Subclass​

public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Toyota", 4, 4);

// Calling the displayDetails method
myCar.displayDetails();
}
}

Explanation:​

  • Superclass (Vehicle): Contains fields brand and wheels, a constructor to initialize these fields, and a method displayDetails() to display vehicle details.

  • Subclass (Car): Inherits the Vehicle class, adds an additional field doors, and overrides the displayDetails() method to include the number of doors.

  • Using the Subclass: An object of the Car class is created, and the displayDetails() method is called to display the details of the car.

The output will be:

Brand: Toyota
Number of Wheels: 4
Number of Doors: 4

Inheritance Concepts​

super Keyword​

  • The super keyword in Java is used to refer to the superclass (parent) object.

  • It can be used to call superclass methods and constructors.

  1. Calling Superclass Constructor: super(brand, wheels);

  2. Calling Superclass Method: super.displayDetails();

Method Overriding​

Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

@Override
public void displayDetails() {
// Overridden method implementation
}

Inheritance Hierarchy​

Inheritance allows you to create a hierarchy of classes, where a subclass can have its own subclasses.

Example of Inheritance Hierarchy​

 class ElectricCar extends Car {
// Additional instance variable
int batteryCapacity;

// Constructor
public ElectricCar(String brand, int wheels, int doors, int batteryCapacity) {
super(brand, wheels, doors);
this.batteryCapacity = batteryCapacity;
}

// Overriding method
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
}
}

Using the Inheritance Hierarchy​

public class Main {
public static void main(String[] args) {
// Creating an object of the ElectricCar class
ElectricCar myElectricCar = new ElectricCar("Tesla", 4, 4, 100);

// Calling the displayDetails method
myElectricCar.displayDetails();
}
}

Explanation:​

  • Superclass (Vehicle): Base class with basic vehicle details.

  • Subclass (Car): Extends Vehicle and adds additional attributes.

  • Sub-subclass (ElectricCar): Extends Car and adds battery capacity attribute, demonstrating a multi-level inheritance hierarchy.

The output will be:

Brand: Tesla
Number of Wheels: 4
Number of Doors: 4
Battery Capacity: 100 kWh

Advantages of Inheritance​

  • Code Reusability: Reuse existing code, reducing redundancy.

  • Method Overriding: Customize or enhance existing methods.

  • Polymorphism: Achieve polymorphic behavior where a subclass instance can be treated as an instance of its superclass.

  • Extensibility: Easily extend existing code with new features.

Summary​

  • Inheritance: Mechanism to derive a new class from an existing class, promoting code reuse and hierarchy.

  • Superclass and Subclass: Superclass is the base class, and subclass inherits from it.

  • extends Keyword: Used to define inheritance.

  • super Keyword: Used to refer to the superclass, call superclass methods, and constructors.

  • Method Overriding: Allows a subclass to provide a specific implementation of a superclass method.

  • Inheritance Hierarchy: Establishes a parent-child relationship across multiple levels.

Understanding inheritance is crucial for creating a hierarchical class structure and reusing code in your Java programs.