Ticker

6/recent/ticker-posts

Inheritance in Java

Inheritance in Java

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

java
class 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

java
public 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

java
public 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

java
public 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:

  1. We start by creating the Vehicle superclass with brand and model properties, along with a constructor to initialize these properties and a displayInfo() method to print the brand and model.

  2. The Car subclass extends Vehicle using the extends keyword. It adds an additional property numDoors and has its constructor to initialize both the superclass properties (brand and model) and its own property (numDoors). It also has a displayCarInfo() method to print the car details, including the number of doors.

  3. The Motorcycle subclass also extends Vehicle and has an extra property hasSidecar. Similar to the Car subclass, it initializes both the superclass properties and its own property through its constructor and provides a method displayMotorcycleInfo() 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:

java
public 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.

Post a Comment

0 Comments