Java Intermediate

0% completed

Previous
Next
Multilevel Inheritance

Multilevel Inheritance is a type of inheritance in Java where a class is derived from a subclass, creating a chain of inheritance. In other words, a subclass becomes the superclass for another subclass. This forms a multilevel hierarchy, allowing the properties and behaviors of multiple classes to be inherited through different levels.

Syntax of Multilevel Inheritance

The syntax for implementing multilevel inheritance involves chaining the extends keyword. Here's the general structure:

Image
class GrandparentClass { // Grandparent class members } class ParentClass extends GrandparentClass { // Parent class members } class ChildClass extends ParentClass { // Child class members }
  • GrandparentClass: The top-level superclass.
  • ParentClass: Inherits from GrandparentClass and acts as a superclass for ChildClass.
  • ChildClass: Inherits from ParentClass.

Example: SportsCar Inheriting from Car, Car Inheriting from Vehicle

In this example, we'll demonstrate multilevel inheritance by creating a Vehicle class, a Car class that inherits from Vehicle, and a SportsCar class that inherits from Car. All classes will be defined within a single Solution.java file for simplicity.

Java
Java

. . . .

Explanation:

  1. Vehicle Class (Grandparent Class):

    • Attributes: brand, model represents general characteristics common to all vehicles.
    • Constructor: Initializes the brand and model of the vehicle.
    • Method: displayVehicleInfo() Displays the vehicle's brand and model.
  2. Car Class (Parent Class):

    • Inheritance: extends Vehicle
      The Car class inherits from the Vehicle class, acquiring its attributes and methods.
    • Additional Attribute: year
      Represents the manufacturing year of the car.
    • Constructor:
      • super(brand, model);
        Calls the superclass (Vehicle) constructor to initialize inherited variables.
      • Initializes the subclass-specific variable year.
    • Method: displayCarInfo()
      Displays the car's manufacturing year.
  3. SportsCar Class (Child Class):

    • Inheritance: extends Car
      The SportsCar class inherits from the Car class, gaining access to its attributes and methods.
    • Additional Attribute: color
      Represents the color of the sports car.
    • Constructor:
      • super(brand, model, year);
        Calls the superclass (Car) constructor to initialize inherited variables.
      • Initializes the subclass-specific variable color.
    • Method: displaySportsCarInfo()
      Displays the sports car's color.

Multilevel Inheritance in Java allows the creation of a hierarchical class structure where a subclass inherits from another subclass, forming a chain of inheritance. By leveraging the extends keyword, each subclass can inherit and build upon the properties and methods of its immediate superclass. This promotes code reusability, logical organization, and scalability within Java applications.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next