In Building an Application, Part 1, you learned about application objects, and that the plans for objects are written into files called classes. In addition, you learned to use predefined classes from the Java API, and to manipulate objects by calling methods, either your own or predefined.
So far, you constructed the first class of the Dive Log application and the place holder classes that DiveLog.java initializes.
Part 2 reinforces these concepts and introduces the
Welcome class, which covers:
In Part 1, you created the
DiveLog class, which contained a constructor that builds the frame for the Dive Log application, a
JMenu, and initializes a
JTabbedPane object with six titled tabs. Each tab creates an object from a placeholder class that, for now, does nothing.
For this part of the tutorial, you need images and the
Welcome.java placeholder class. You can use different images than those provided here, but to prevent problems with layout make them the same size as the images provided. As you work through the tutorials, you'll discover more ways to customize your application and its layout.
Note: It's assumed you installed the Java TM 2 Platform, Standard Edition on your system, and that you have completed Part 1 of the Building an Application tutorial.
|More on Inheritance What is Inheritance?
Examples of Inheritance
Applications can consist of a single class, but most are built with many classes. The classes that make an application often communicate through a reference to an object and methods, using the dot operator. You've seen examples of this in the
In this example,
dlframe is the reference to the instance of a
JFrame object you created, but the
JFrame class doesn't define a method called
setSize. So, where does this method come from? How can you call a method on a
JFrame object, when the
JFrame class doesn't define that method? This process works similar to the way your hair color is passed down to you through your mother or father--through inheritance. But Java class inheritance gives a developer much more control over the child object than human inheritance does.
Because you instantiated an object of type
DiveLog class inherited all the methods that
JFrame contains. In addition, the
DiveLog class inherited the methods that
JFrame inherited. The
JFrame class inherits methods and fields from several classes up the hierarchy tree:
All classes inherit from class
Object automatically. In addition, when you create an object of the
JFrame type, this new object also inherits from the
Component classes. To call a method from one of these inherited classes, you generally use the dot operator with your reference variable. The
setSize method was inherited from the
Not all inherited methods and fields are accessible, yet they are part of the make-up for that object. Later, you'll learn more about accessing certain types of data from parent classes.
There is a more direct way to inherit from specific classes: use the
extends keyword in the class declaration. By using
extends, your child (also called subclass or derived class) inherits from the parent or super class(es) and frees you from having to:
In other words, the
extends keyword allows your class to inherit from a class of your choosing, unlike human inheritance in which you have no choice of who your parents are or what traits you inherit from them.
To make the
DiveLog child class a child of the
JFrame parent class, you type:
public class DiveLog extends JFrame
Now you have specified a class you want your subclass to inherit from, and it becomes that type of class. Using the above statement, you make the
DiveLog class a type of
JFrame object, just like a Labrador puppy is a Labrador type of dog.
By extending to the the
JFrame class, there is no need to instantiate
JFrame in your class to get to its methods, as shown in the previous lesson:
dlframe.addWindowListener(new WindowAdapter() dlframe.getContentPane().add(tabbedPane); dlframe.setJMenuBar(mb);
|Overriding Methods A child class can change the behavior of any inherited method by overriding that method.
See Overriding Methods
Instead, you can write the method call without the variable
dlframe. You call the inherited methods by their names:
getContentPane().add(tabbedPane); addWindowListener(new WindowAdapter() getContentPane().add(tabbedPane); setJMenuBar(mb);
The parent class,
JFrame, has a constructor you can call by using the
DiveLog can call the parent
JFrame constructor and supply the
String to appear at the top of the frame window as follows:
super("A Java Technology Dive Log");
In human inheritance, you get some genes from your mother and some from your father. With class inheritance, by using the
extends keyword, the
DiveLog object is a
JFrame object with additional features that you added. In other words, the
DiveLog object has everything a
JFrame object has and more. In addition to having a frame with a title, the
DiveLog object, derived these from the
JFrame object, has:
To improve this class, you can move the
setVisible(true) methods into the
main method. Since
DiveLog is a type of
frame object, it makes sense to set size and background on the newly instantiated
DiveLog object once it's built, rather than when it's being constructed. But either way works.
You don't need to rewrite your
DiveLog.java to continue with this tutorial, but it is a good exercise in inheritance.
Note: You must be in the
The next Dive Log class
extends a class to gain the benefits of inheritance. The
Welcome class holds the content for the first tab in the Dive Log application.
|Which class should the