Introduction
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (subclass or child class) to inherit properties and behaviors from another class (superclass or parent class). It promotes code reuse, extensibility, and hierarchical organization of classes. In Java, inheritance is achieved using the extends
keyword.
Syntax
javaclass Subclass extends Superclass {
// Subclass members
}
Example:
Let's illustrate inheritance with a simple example of a Vehicle
superclass and two subclasses Car
and Motorcycle
.
Step 1: Create the Vehicle
Superclass
javapublic class Vehicle {
protected String brand;
protected String model;
public Vehicle(String brand, String model) {
this.brand = brand;
this.model = model;
}
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
}
}
Step 2: Create the Car
Subclass
javapublic class Car extends Vehicle {
private int numDoors;
public Car(String brand, String model, int numDoors) {
super(brand, model);
this.numDoors = numDoors;
}
public void displayCarInfo() {
System.out.println("Car Details:");
displayInfo();
System.out.println("Number of Doors: " + numDoors);
}
}
Step 3: Create the Motorcycle
Subclass
javapublic class Motorcycle extends Vehicle {
private boolean hasSidecar;
public Motorcycle(String brand, String model, boolean hasSidecar) {
super(brand, model);
this.hasSidecar = hasSidecar;
}
public void displayMotorcycleInfo() {
System.out.println("Motorcycle Details:");
displayInfo();
System.out.println("Has Sidecar: " + hasSidecar);
}
}
Explanation:
We start by creating the
Vehicle
superclass withbrand
andmodel
properties, along with a constructor to initialize these properties and adisplayInfo()
method to print the brand and model.The
Car
subclass extendsVehicle
using theextends
keyword. It adds an additional propertynumDoors
and has its constructor to initialize both the superclass properties (brand
andmodel
) and its own property (numDoors
). It also has adisplayCarInfo()
method to print the car details, including the number of doors.The
Motorcycle
subclass also extendsVehicle
and has an extra propertyhasSidecar
. Similar to theCar
subclass, it initializes both the superclass properties and its own property through its constructor and provides a methoddisplayMotorcycleInfo()
to display motorcycle details, including whether it has a sidecar or not.
Usage:
You can use inheritance to create specialized classes that inherit common attributes and methods from a more general class. For example:
javapublic class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota", "Corolla", 4);
myCar.displayCarInfo();
Motorcycle myMotorcycle = new Motorcycle("Harley-Davidson", "Road King", true);
myMotorcycle.displayMotorcycleInfo();
}
}
Output:
Car Details:
Brand: Toyota
Model: Corolla
Number of Doors: 4
Motorcycle Details:
Brand: Harley-Davidson
Model: Road King
Has Sidecar: true
In this example, the Car
and Motorcycle
subclasses inherited the displayInfo()
method from the Vehicle
superclass, and they added their specific details to it. This demonstrates how inheritance facilitates code reuse and provides a clear class hierarchy.
0 Comments