New t

Managing Inheritance

o Java

New to Java Programming Center

New to Java

Programming Center

Java Platform Overview | Getting Started | Learning Paths
References & Resources | Certification | Supplements




Contents
Inheritance | NEXT>>Containers and Components

Download or Print -->

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:

  • Inheritance
  • Images and text objects
  • String concatenation
  • Layout managers

Getting Started

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.


  1. Save the following images to the divelog/images directory:
  2. Or create images of your own, but use the same pixel sizes as those listed above.


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.

Inheritance

 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 DiveLog class:

dlframe.setSize(765, 690);

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 JFrame, the 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:

JFrame Class Hierarchy

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 Frame, Window, Container, and 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 Component class.

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:

  • Instantiate that desired inherited class to get to its methods and fields.
  • Call a method from the super class with a variable reference and dot operator.

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 poarent 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.

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 super keyword. 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:

  • a TabbedPane object
  • tabs with titles
  • memory reserved from place holder classes where you create more objects.

To improve this class, you can move the setSize(765, 690), setBackground(Color.white), and 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.


  1. Open DiveLog.java in your editor.
  2.  
  3. Change the class from:


  4. public class DiveLog
    


    to:

    public class DiveLog extends JFrame
    
  5. Delete the variable declaration: private JFrame dlframe;
  6. Change the old JFrame constructor:
  7. JFrame dlframe = 
    new JFrame("A Java Technology Dive Log");
    


    to:

    super("A Java Technology Dive Log");
    
  8. Remove the dlframe. from the methods.
  9.  
  10. After initializing a DiveLog object in main, use the dl variable to call the setSize(765, 690), setBackground(Color.white), and setVisible(true) methods.


  11. For example:

    dl.setSize(765, 690);
    
  12. Save the file.
  13.  
  14. Compile the DiveLog.java class with:


  15. On a Windows platform:

    C:\divelog>javac -classpath C:\ DiveLog.java 
    


    In the Solaris operating environment:

    divelog% javac -classpath /home/usr/ DiveLog.java
    


Note: You must be in the divelog directory while running this command. Make sure there is a space between the last \ or / before DiveLog.java.

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 Welcome class extend?
   A.  JFrame
   B.  JPanel
   C. Neither