The extends keyword

The extends keyword in java is used to inherit the features of super class

The extends keyword

The extends keyword

In this section you will learn about extends keyword in java. The extends is a keyword in java which is used in inheritance. It is used to specify super class in a class declaration. Using this keyword a subclass is inheriting the features of super class. It cannot be used as a variable or identifier. In Java, every class is subclass of  java.lang.Object class.

For example, class B want to inherits the features of class A using extends keyword, either by adding field or overriding method in subclass. Take a look at the following example which illustrate the use of extends keyword in java.

 class A
  {
   public int a=5;;
   }
   class B extends A
    {
      public void display()
      {
       a++;
      }
     }  
      

In the above code you can see that class B extends class A means it inherits the feature of class A. Inheritance by extends keyword can either be done by adding field or either by adding  function of super class.

The extends keyword is also used to implements interface. Using extends keyword, one interface can extend another interface. But class can only extends another class using extends keyword, but if a class want to access the member of interface or defining a method of interface in a class, it must use implement keyword.

Example:  Using extends keyword one interface implementing another interface.

public interface A 
 { }
  interface B extends A  { }