Home Help Java E The extends keyword
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

The extends keyword

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

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  { }
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 13, 2013

Ask Questions?    Discuss: The extends keyword  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com