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

 
 
Content Index
Content Page # 14

Over-riding methods and constructors

Providing a method in a subclass with the same name (and parameters) as in its base class is called overriding the method. The subclass method takes precedence over the base class method. This is usually the desired effect, except in constructors. The super method invokes the base class constructor.

Suppose I define a class called Button, which displays a button on the screen. The button is (for example) grey. The user can click the button and cause the program to carry out an action. The Button has a method called paint() which causes it to update its screen display. Now suppose I want to create a coloured button. Let's call this class ColouredButton . A ColouredButton is exactly the same as a Button except that :

  • the paint() method paints a coloured button rather than a grey one, and
  • when we create a ColouredButton we want to specify its colour
So to create an ordinary button we might write: Button myButton = new Button ("OK"); and for a coloured button we might write ColouredButton myButton = new ColouredButton ("OK", Color.red); So I will define my class ColouredButton something like this: class ColouredButton extends Button
{
  Color thisButtonColour;
  // constructor
  public ColouredButton(String text, Color colour) 
  {
    thisButtonColour = colour;
    // What do we do with `text'?
  }
  public void paint(Graphics g)
  {
      // Statements to draw the button in the new colour
  }

  }

In the constructor for ColouredButton we save the specified colour in a variable so that the paint() method can use it to determine what colour to draw the display in. However, what do we do with the `text' argument? This defines the text that will appear on the button. Now we could save this as an variable for later use, but this would be inefficient, since the class Button also has text, so the Button base class of ColouredButton probably needs to know what text is being displayed as well.

If I had created a new Button object, I would have written:

Button myButton = new Button ("OK"); This indicates that the constructor for Button handles the text that the button will display. But in ColouredButton we have over-ridden the constructor with a new constructor. Normally when we over-ride a method we want the new method to replace the old one. However, with constructors we usually want the new constructor to add to the old one. 

Java has a keyword for this: super . This means `call the constructor for this class's super (base) class'. The super constructor takes the same arguments that the constructor itself would take. So in the Button example, when we call super in the ColouredButton constructor, we specify the text of the button just as if we were creating a new Button object. So the constructor for ColouredButton becomes:

public ColouredButton(String text, Color colour) 
{
  super(text); // call base class constructor
  thisButtonColour = colour;
}
If you create a subclass of a class that was not written by yourself (e.g., one of Java's many built-in classes), it is good practice to call super at the start of any constructor that your new class over-rides. This will ensure that the base class is initialised correctly. Failure to do this in Java leads to all sorts of bizarre errors which are hard to detect.

Back to top

basicline.gif (169 bytes)

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