Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 10, 2025

Inheritance; Super classes, sub classes.

 

Inheritance in Java

Inheritance is a fundamental concept of Object-Oriented Programming (OOP) that allows one class to inherit the properties and behavior of another class. It enables code reusability and method overriding.


1. What is Inheritance?

Inheritance allows one class (subclass/child class) to inherit the attributes and methods of another class (superclass/parent class).

  • Super Class (Parent Class): The class whose features are inherited.
  • Sub Class (Child Class): The class that inherits the properties and behaviors of the superclass.

2. Benefits of Inheritance

  1. Code Reusability: Avoids redundancy by reusing existing code.
  2. Extensibility: Allows the creation of new functionalities by enhancing the parent class.
  3. Method Overriding: Allows child classes to provide specific implementations for methods already defined in the parent class.
  4. Improved Maintainability: Changes made in the parent class are reflected in child classes, enhancing maintainability.

3. Syntax of Inheritance

In Java, inheritance is implemented using the extends keyword.

class SuperClass {

    // Superclass code

}

 

class SubClass extends SuperClass {

    // Subclass code

}


4. Example of Inheritance

Let's create a real-world example of inheritance where a Vehicle class is the parent class, and Car and Bike are child classes.

4.1 Superclass (Vehicle)

// Superclass

class Vehicle {

    String brand;

 

    public Vehicle(String brand) {

        this.brand = brand;

    }

 

    public void display() {

        System.out.println("Brand: " + brand);

    }

 

    public void start() {

        System.out.println("Vehicle is starting...");

    }

}


4.2 Subclass (Car)

// Subclass inheriting Vehicle

class Car extends Vehicle {

    int numberOfDoors;

 

    public Car(String brand, int numberOfDoors) {

        super(brand); // Calling the superclass constructor

        this.numberOfDoors = numberOfDoors;

    }

 

    @Override

    public void start() { // Method Overriding

        System.out.println(brand + " Car is starting...");

    }

 

    public void display() {

        super.display(); // Calling the superclass method

        System.out.println("Number of Doors: " + numberOfDoors);

    }

}


4.3 Subclass (Bike)

// Subclass inheriting Vehicle

class Bike extends Vehicle {

    boolean hasCarrier;

 

    public Bike(String brand, boolean hasCarrier) {

        super(brand);

        this.hasCarrier = hasCarrier;

    }

 

    @Override

    public void start() { // Method Overriding

        System.out.println(brand + " Bike is starting...");

    }

 

    public void display() {

        super.display();

        System.out.println("Has Carrier: " + hasCarrier);

    }

}


4.4 Testing the Inheritance

public class Main {

    public static void main(String[] args) {

        Car myCar = new Car("Toyota", 4);

        myCar.display();

        myCar.start();

 

        System.out.println();

 

        Bike myBike = new Bike("Honda", true);

        myBike.display();

        myBike.start();

    }

}


4.5 Output

Brand: Toyota

Number of Doors: 4

Toyota Car is starting...

 

Brand: Honda

Has Carrier: true

Honda Bike is starting...


5. Important Points to Remember

  1. Single Inheritance: Java supports single inheritance, i.e., a class can inherit from only one superclass.
  2. Multilevel Inheritance: A class can inherit from another subclass, forming a hierarchy.
  3. Hierarchical Inheritance: One superclass can be inherited by multiple subclasses.
  4. Method Overriding: Child classes can provide their own implementation of inherited methods.
  5. super Keyword: Used to access superclass members when overridden by the subclass.
  6. Protected Access Modifier: Allows visibility to subclasses.

6. Example of Multilevel Inheritance

// Base Class

class Animal {

    void eat() {

        System.out.println("Animal is eating.");

    }

}

 

// Intermediate Class

class Dog extends Animal {

    void bark() {

        System.out.println("Dog is barking.");

    }

}

 

// Derived Class

class Labrador extends Dog {

    void display() {

        System.out.println("Labrador is a breed of Dog.");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Labrador lab = new Labrador();

        lab.eat();

        lab.bark();

        lab.display();

    }

}


Output

Animal is eating.

Dog is barking.

Labrador is a breed of Dog.


 

No comments:

Post a Comment