Java extends Keyword with Examples

In this article, we will discuss one of the most important and widely used keywords in Java which is the extends keyword. It is a reserved word that and we can’t use it as an identifier other than the places where we need to inherit the classes in Java. We use the extends keyword in Inheritance in Java. Inheritance is one of the object-oriented concepts which defines the ability of a class to extend or acquire the properties of another class. The extends keyword plays a significant role in implementing the Inheritance concept in Java. Let’s start discussing the extends keyword with examples.

Java extends keyword

 

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

Java extends Keyword

The extends keyword in Java indicates that the child class inherits or acquires all the properties of the parent class. This keyword basically establishes a relationship of an inheritance among classes. If a class extends another class, then we say that it has acquired all the properties and behavior of the parent class.

We use the extends keyword in Java between two class names that we want to connect in the Inheritance relationship. The class that extends the properties is the child class or the derived class which comes before the extends keyword, while the class from which the properties are inherited is the parent class or superclass or the base class, and this class comes after the extends keyword.

It is not possible to extend multiple classes in Java because there is no support for multiple inheritances in Java. And therefore we cannot write multiple class names after the extended keyword. But, multiple classes can inherit from a single class as java supports hierarchical inheritance. Therefore we can write multiple class names before the extended keyword.

Syntax of extends keyword in Java

Following is the syntax of using extends keyword in java when using inheritance:

class Parent {
  //code inside the parent class
}
class Child extends Parent {
  //Code inside the child class
}

The two important categories are:

a. Parent class- This is the class being inherited. Also called superclass or base class.

b. Child class- This class inherits the properties from the parent class. Also called a subclass or derived class.

Example of Java extends keyword

Let’s see an example that uses the extends keyword in Java. The child class acquires all the accessible properties from its parent class and can also add some additional properties inside it to provide more information. Therefore when we access this class, we get all the properties from the parent class as well as the child class.

For example, we have a Calculator class that has methods named add() and subtract(). Now we create a class called MyCalculator that extends the Calculator class. We want to add the function of multiplication and modulus in it, then we can add two methods named multiply() and modulus(). Now when we access this class, then we can utilize all the four functionalities named-add(), subtract(), multiply() and modulus(). Let’s see the implementation of this example:

Code to understand Java extends:

package com.techvidvan.extendskeyword;
class Calculator {
  public int result;

  public void add(int num1, int num2) {
    result = num1 + num2;
    System.out.println("The sum of the given numbers: " + result);
  }
  public void subtract(int num1, int num2) {
    result = num1 - num2;
    System.out.println("The difference between the given numbers: " + result);
  }
  public void multiply(int num1, int num2) {
    result = num1 * num2;
    System.out.println("The product of the given numbers: " + result);
  }
}
class MyCalculator extends Calculator {
  public void divide(int num1, int num2) {
    result = num1 / num2;

    System.out.println("The division of the given numbers: " + result);
  }
  public void modulus(int num1, int num2) {
    result = num1 % num2;
    System.out.println("The remainder of the given numbers: " + result);
  }
}
public class ExtendsDemo {
  public static void main(String args[]) {
    int number1 = 25,
    number2 = 10;
    MyCalculator object = new MyCalculator();
    object.add(number1, number2);
    object.subtract(number1, number2);
    object.multiply(number1, number2);
    object.divide(number1, number2);
    object.modulus(number1, number2);
  }
}

Output

The sum of the given numbers: 35
The difference between the given numbers: 15
The product of the given numbers: 250
The division of the given numbers: 2
The remainder of the given numbers: 5

Extending Final Class in Java

If a class declares as final, then we can’t extend or inherit it using the extends keyword. If we try to extend the final classes, then we will get a compilation error. We can make the class as final when we do not want any class to inherit its properties. For example:

Code to understand that we cannot extend the final class:

package com.techvidvan.extendskeyword;
final class Parent {
  public int number1 = 5;
  public int number2 = 10;

  public void display() {
    System.out.println("The first number is: " + number1);
    System.out.println("The second number is: " + number2);

  }
}
class Child extends Parent {
  int result;
  public void sum() {
    result = number1 + number2;
    System.out.println("Sum of two numbers is: " + result);
  }
}
public class Demo {
  public static void main(String args[]) {
    Child c = new Child();
    c.display();
    c.sum();
  }
}

Output

Demo.java:12: error: cannot inherit from final Parent
class Child extends Parent
^If we remove the final keyword, then we will get the output:
The first number is: 5
The second number is: 10
Sum of two numbers is: 15

Extending Interfaces in Java

The extends keyword can also be used to inherit the interfaces in java similar to that of classes. As one class can extend another class, similarly an interface can also extend interface in java. The child interface inherits the methods of its parent interface.

Code snippet to demonstrate the extending of interfaces using extends keyword:

// Filename: Item.java

public interface Item {
  public void setItemId(int id);
  public void setItemName(String name);
}

// Filename: Laptop.java

public interface Laptop extends Item {
  public void modelNumber(int number);
  public void brandName(String brandName);
  public void price(double price);
}

Conclusion

So, here we come to the end of this article of extends keyword in Java. You might have understood the significance and usage of the extends keyword in Java. We can extend both classes and interfaces in Java using the extends keyword. We have learned the keyword with real-life examples.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.