Inheritance in Java

Inheritance in Java is one of the basic concepts of Java OOP. It is a mechanism that ensures that one class inherits all the attributes (fields) and behaviours (methods) of another class.

The idea behind inheritance is that new classes can be created that completely take over the functionality of the existing class.

The new class will contain all the fields and methods from the existing class, which it inherited (except those declared with the private modifier), and may have its own defined fields and methods.

In inheritance, the existing class becomes the parent class (superclass), and the new class that inherits the existing one becomes the child class (subclass). As in real life, a child usually inherits many traits from their parent.

Note: A class can inherit only one parent class, and each class can have an unlimited number of child classes (subclasses).

Inheritance is ensured by the keyword extends, and the syntax looks like this:

public class SubClass extends SuperClass {
  
  // fields
  
  // methods
}


Let’s look at an example that illustrates the concept of inheritance.

Inheritance Code Example in Java

Let’s say we have classes Vehicle and Car.

Since a car is a vehicle in real life, we create classes in a way that Car class extends the Vehicle class:

class Vehicle {

  private String brand;

  // a superclass constructor that can't be inherited
  public Vehicle(String brand) {
    this.brand = brand;
  }

  public void printBrand() {
    System.out.println("Brand is: " + brand);
  }

  // overriding the toString() method of a superclass Object
  public String toString() {
    return "This is a " + brand;
  }

}

class Car extends Vehicle {

  // car has its own attributes declared
  private int numberOfSeats;
  private String colour;

  //constructor of the subclass
  public Car(String model, int numberOfSeats, String colour) {
    super(model);  // call the constructor of the superclass to set the brand field
    this.numberOfSeats = numberOfSeats;
    this.colour = colour;
  }

  // Method which returns custom string for the Car
  public String printCar() {
    return super.toString() + " with a number of seats " +
            numberOfSeats + " and with colour " + colour;
  }
}

public class Test {

  public static void main(String[] args) {
    Car car = new Car("Ford", 5, "blue");

    car.printBrand(); // calling the inherited method

    System.out.println(car.printCar());

  }

}
Output: Brand is: Ford This is a Ford with a number of seats 5 and with colour blue

In the above example, the Car inherited method printBrand(), so even though we didn’t declare it in the Car class, we could use the vehicle class (superclass) version.

Notes:

  • Unlike fields and methods, a subclass cannot inherit constructors of a superclass.
  • The constructor of a superclass can only be called from the subclass constructor using the super keyword.
  • All classes extend java.lang.Object class by default. This ensures that the classes we define inherit members of the Object class.

To learn more, check out Java Tutorials for Beginners page. 

Leave a Reply

Your email address will not be published.