Home Help Java E The extends Keyword

Related Tutorials


 
 

Share on Google+Share on Google+

The extends Keyword

Advertisement
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 {

  ??????.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: October 10, 2006

Related Tutorials

Discuss: The extends Keyword  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
Jordan Disko
February 22, 2012
Extends

Can you extend from a class in a separate Java file? Or do all parents need to be included in the extends file? If so, how do you associate the parent and child other than calling one from the other? How to do in eclipse?
Rob
March 28, 2012
extends keyword

can it be that the extends keyword have more than one (1) argument?? for example public class A extends B,C...
K.sabari nathan
October 26, 2012
c,c++,java

i want to learn programming languages completely
Jenny Angcon
November 26, 2012
computer programming

i dont understand your examples given
DMCA.com