您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Content Index
Content Page # 5

Java keyword 慹xtends?

In Java, the keyword extends denotes full inheritance. If a definition states that a new class 'extends' another, this new class will inherits all the non-private methods (and variables) of the class it extends 

To change a method in some way is called overriding the method.

For example, suppose the class Shape has a method called draw() . If we define Line as being a type of Shape as below: 

class Line extends Shape
{
  // variables (none at present)
  // methods (none at present)
}
This means that class Line is a direct subclass of class Shape .

Then we could issue the instructions: 

Line aLine = new Line();
aLine.draw();
this would send a message to the object aLine , which will invoke the draw() method in Shape , because we have not defined a draw() method for Line (in fact, we have not defined any methods for Line !) 
However, we now define a draw method in the class Line class Line extends Shape
{
  public void draw()
  {
    // some actions to draw a line
  }
  // other methods go here...
}
When we do: aLine.draw()
this message will result in the invocation of the draw() method defined in class Line , not the one in Shape . Thus we have overridden the inherited method Shape.draw() in the class Line
Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg