Home Help Java E The extends Keyword



The extends Keyword
Posted on: October 10, 2006 at 12:00 AM
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 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 {

  ??????.

 

Related Tags for The extends Keyword:


More Tutorials from this section

Ask Questions?    Discuss: The extends Keyword  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.