home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Using the Super() Method

In Java terminology, the existing class is called the superclass. The class derived or extended from the superclass is called the subclass. Sometimes a superclass is referred to as a parent class, or a base class, and a subclass is referred to as a child class, an extending class, or a derived class. You can reuse or change the methods of superclasses. You can add new data and new methods in the subclasses. Subclasses usually have more functionality than their superclasses.

The super() method refers to the superclass of a class in which the super() method appears. This method can be used in two ways: to call a superclass constructor or to call a superclass method.

Calling Superclass Constructors

The syntax to call a superclass constructor is as follows:

super( parameters );

In the BetterShape class, for example, the super() method can be used to call the Shape class’s constructor. Anytime you use the super() method in the constructor, it has to appear in the first line of the constructor, and it is the only way to invoke a superclass’s constructor.


Caution:  Java requires the super() method to appear first in the constructor, even before data fields. It also requires using the keyword super to call the superclass’s constructor.

Calling Superclass Methods

The keyword super can be used to reference a method other than the constructor in the superclass. The syntax can look like this:

super.method( parameters );

It is not always necessary to use the super keyword if you’re calling one of the superclass’s methods. If the subclass does not have a method matching the same name, all you have to do is call the superclass method you want to use. If, however, both the superclass and the subclass have methods of the same name, don’t worry. From within the subclass method (or anywhere, really), you can make a call to the superclass’s method by inputting super.method() as shown in the preceding example.

Calling with the this Keyword

The keyword super is used to reference superclasses. Occasionally, you need to reference the current class. Java provides another keyword, this, for referencing the current object. Use of the this keyword is analogous to use of super. For instance, in the preceding section we talked about calling super.method. You can also call this.method(), or you can access this.method() to call methods in the current class. The following syntax line shows how to use the this keyword to call a method in the current class:

this.method( parameters );

Creating a Program That Uses an Extended Frame Class

We’re going to keep things simple! We’re going to create an applet that declares a class called MyFrame. The MyFrame class is extended from the Frame class. It doesn’t do anything more than add its own paint() method. You can use this paint() method, then, to draw anything you want in the Frame window. The source-code listing for this applet is given in Listing 5.3. You can see the applet running in Figure 5.4. Note that if you look at the figure, you’ll see two windows: the Main applet window and the Frame window, which is above the applet window.

Listing 5.3 The MyFrame Applet

1   import java.awt.*;
2   import java.applet.*;
3
4   public class Applet1 extends Applet
5   {
6       // Declare and create the MyFrame object.
7       MyFrame m_MyFrame = new MyFrame( "My Frame" );
8
9       public void init()
10      {
11          // Set the size of the window.
12          m_MyFrame.setSize( 200, 200 );
13          // Make the window visible.
14          m_MyFrame.setVisible( true );
15      }
16
17      public void paint( Graphics g )
18      {
19          // Draw to the applet window.
20          g.drawString( "Drawing to the applet", 20, 20 );
21      }
22  // Declare a new class that extends the Frame class.
23      public class MyFrame extends Frame
24      {
25          public MyFrame( String s )
26          {
27              // Call the Frame constructor.
28              super( s );
29          }
30
31          public void paint( Graphics g )
32          {
33              // Draw to the Frame window.
34              g.drawString( "Drawing to the frame", 20, 50 );
35          }
36
37      }
38
39  }


Figure 5.4  An applet that’s using an extended Frame class.

Listening for Frame Class Events

Earlier today, we talked about the problem of the Frame window not closing when you clicked the Close button. To respond to events such as the user clicking the Close button, we must create a class inside of the Frame class that will listen for such events. The class we create will extend the WindowAdapter class. (And you were wondering why we spent time today talking about classes and how to extend them!) After we declare this, we can override the windowClosing() method and do whatever we want, which is to close our window. Now the last step! Inside of the Frame class’s constructor, call the addWindowListener() method and add the extended WindowAdapter class.

These are the steps necessary to listen for frame window events:

1.  In your Frame subclass, declare a class that extends the WindowAdapter class.
2.  In the WindowAdapter class, override any methods you want to respond to.
3.  Somewhere in the Frame subclass, use the addWindowListener() method to add your new class so that it will receive messages as they come in.

Seven different events are available to you when you have a class that’s extended from the WindowAdapter class. They are windowActivated(), windowClosed(), windowClosing(), windowDeactivated(), windowDeiconified(), windowIconified(), and windowOpening() methods. Table 5.1 shows you all these methods.

Table 5.1 The WindowAdapter Class’s Events

Name Description

windowActivated() This event is triggered when the window gets the focus.
windowClosed() This event is fired after the window is closed.
windowClosing() This event is triggered in response to a user’s request to close a window, such as clicking the Close button.
windowDeactivated() This event is triggered when the window loses the focus.
windowDeiconified() This event is not available on all platforms. It’s triggered when users attempt to minimize the window.
windowIconified() This method is also not available on all platforms. It’s triggered when users attempt to unminimize the window.
windowOpening() This event is triggered when a window is first opened.

You need to remember one more thing when you’re trying to get events from Frame windows. You need to import Java.AWT.event (line 3 in Listing 5.4). To create the example in Listing 5.4, I simply took the preceding example and followed the three steps for event handling. Notice that in line 3, I imported Java.AWT.event. Also notice that in line 32, I made a call to the addWindowListener() method. This is what actually adds the WindowListening class to the event cue. Now look at line 42. This is where my WindowListening class begins. The only method I’m listening for is the windowClosing() method, which starts at line 45. You can see at line 48 that the window’s visibility is set to false, which hides it. Then the applet sets the parent class’s m_MyFrame variable to null. Now, when you click the Frame window’s Close button, it will actually close.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.