Java

How to extend Two Classes in Java

Java supports an OOPs feature called inheritance, that allows extending a parent class to other child classes in order to access properties of a parent class. There is a restriction in Java related to inheritance, referring to a class that can only extend to another class at once. Extending more than one class will not execute the program successfully.

This writeup will demonstrate the method for extending two classes in Java.

How to Extend Two Classes in Java?

In Java, we can extend two classes by using the “extend” keyword, which means that the child class will inherit all of the parent class’s properties.

Syntax
To extend Java two classes, follow the given syntax:

Child extend Parent

Here, Child represents the child class, and Parent denotes the Parent class.

Example 1: Extend a Class in Java
In this example, we will extend a class “Example” as a child class from a parent class named “Random” to access their properties. The Random class contains a static integer type method named “sum()” with two integer type parameters “a” and “b”. This method will return the sum of the specified numbers:

class Random{
    public static int sum (int a, int b) {
        int sum = a+b;
        return sum;
    }
}

We will extend the Example class to the Random class and print the value of two numbers by calling the sum() method of the parent class Random and pass the required integer values as argument:

public class Example extends Random {
    public static void main(String[] args) {
         System.out.println("Sum of two numbers is " + sum(30,45));
  }
}

The output shows the sum of two numbers as we have successfully accessed the parent’s class method:

Example 2: Extend Two Classes Simultaneously in Java
Java supports single inheritance but not multi-inheritance. In single inheritance, the classes extend from only one class called the parent class, while in multi-inheritance, the child class is extended to multiple classes, which is not possible.

For example, class B is extended with class A, and classes B and C are both extended from class A. This is the single inheritance which shows that class A has two child classes, B and C. While if C class gets extended from classes A and B, it implies that class C has two parents, class A and class B. This is not possible in Java; trying it out will result in an error.

Here, in this example, we will create three classes, “Vehicle”, “Car”, and “Bike”. The class Car is extended with classes Vehicle and Bike, which gives an “Unresolved compilation error”:

Example 3: Extend Multiple Classes with Single Inheritance
By using single inheritance, we will extend two or more classes with one class. In this example, we have a class Vehicle that is the parent class, and the classes Car and Bike are extended from it. The Vehicle class contains a method “speed()” with an integer type parameter named vSpeed that prints the speed of the vehicle. Because of inheritance, the speed method is also accessible by child classes:

class Vehicle{
    void speed(int vSpeed) {
        System.out.println("Current Speed : "+vSpeed);
    }
}

The child class Car contains an integer type variable “cSpeed” and the method “color()” with a string type variable “color” as a parameter:

class Car extends Vehicle{
    public int cSpeed;
    void color(String color) {
        System.out.println("Color of car is: "+ color);
    }
}

The child class Bike contains an integer type variable “bSpeed”:

class Bike extends Vehicle{
    public int bSpeed;
}

In the main() method of another class named Example, we create an object of the Car child class and access the parent’s class method “speed()” with the object of the child class “Car”:

public class Example {
    public static void main(String[] args) {
         Car car = new Car();
         car.speed(30);
         car.color("Black");
  }

The output indicates that the child class successfully accessed the method of the parent class by using single inheritance:

We gathered all the basic instructions related to how to extend two classes in Java.

Conclusion

Two classes extend in Java by using the “extend” keyword. Extending classes means we are inheriting all of the Parent class’s properties to the Child class. By using the extend keyword, one can easily access the variables and methods of the parent class in the child class. In Java, you can achieve single inheritance by using the extend keyword, while Java does not support the multi-inheritance/ In this writeup, we demonstrated the method to extend two classes in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.