The extends Keyword

The extends is a Java keyword, which is used in inheritance process of Java. It specifies the superclass in a class declaration using extends keyword.

The extends Keyword

The extends Keyword

     

The extends is a Java keyword, which is used in inheritance process of Java. It specifies the superclass in a class declaration using extends keyword. It is a keyword that indicates the parent class that a subclass is inheriting from and may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program. In Java, every class is a subclass of java.lang.Object. 

For example, Class X extends class Y to add functionality, either by adding fields or methods to class Y, or by overriding methods of class Y.

Take a look at the following example, which demonstrates the use of the 'extends' keyword. 

public class A {

  public int number;

class B extends A {

  public void increment() {

  number++;

  }

} 

 In this example, we inherit from class A, which means that B will also contain a field called number. Two or more classes can also be inherited from the same parent class.

extends keyword is also used in an interface declaration to specify one or more superinterfaces.

For instance: 

interface MyInterface {

  ????.



interface MyInterface extends SuperInterface {

  ??????.