Java Platform Packages Used In This Book


Navigating A Class Inheritance Hierarchy

Let’s dwell for a moment on the inheritance hierarchy information and how it will help you to know how to read, navigate, and interpret the inheritance hierarchy of a particular class.

All classes supplied by the Java platform inherit their functionality from one or more classes contained within the Java platform. The only exception to this rule is the java.lang.Object class which serves as the ultimate base class for all reference types.

In Java, classes can inherit the functionality of only one class, but may implement as many interfaces as necessary. The class it inherits from may also inherit its functionality from another class, and so on. What this means to you is that a class’s total functionality is the sum of all the public and protected methods it inherited from its base class plus any additional functionality it provides. Thus, to discover all the functionality provided by a particular class you must navigate up the inheritance hierarchy chain, visiting each class of the hierarchy in turn.

Let’s look at two examples. The first one is the java.lang.String class shown previously in figure 5-4. It inherits from the java.lang.Object class. So, the functionality provided by the String class is whatever functionality the Object class provides plus whatever functionality the String class adds or overrides. The String class also implements three interfaces: CharSequence, Comparable, and Serializable.

The inheritance hierarchy of the String class is shown in figure 5-5 as a Unified Modeling Language (UML) class diagram. By reading this class diagram you can immediately determine that a String is an Object and a CharSequence. It is also Comparable and Serializable. What this means from an object-oriented point of view is that anywhere in a Java program you can use an object of type Object, CharSequence, Comparable, or Serializable you can substitute a String object because it implements the functionality of all these types.

image from book
Figure 5-5: String Inheritance Hierarchy UML Diagram

The second example is a little more complex but easy to understand now that you know how the String inheritance hierarchy works.

Some of the more complex inheritance hierarchies are found in the Java Foundation Class packages of java.awt and javax.swing. The classes in these packages are used to create rich graphical user interfaces (GUIs). Let’s take a look at the javax.swing.JButton class inheritance hierarchy as is shown in figure 5-6

image from book
Figure 5-6: JButton Inheritance Hierarchy Information

As you can tell from reading the inheritance hierarchy information provided in figure 5-6 the JButton class inherits functionality from javax.swing.AbstractButton, which inherits from javax.swing.JComponent, which inherits from java.awt.Container, which in turn inherits from java.awt.Component, which ultimately inherits from java.lang.Object. It also implements the Accessible, ImageObserver, ItemSelectable, MenuContainer, Serializable, and SwingConstants interfaces. In other words, a JButton has a lot of functionality. To understand what a JButton can do you must understand what functionality is provided by all the base classes in the inheritance chain. You must also understand what it means to implement the various interfaces. All this information is available in the Java platform API documentation.

Figure 5-7 shows the UML class diagram for the JButton inheritance hierarchy. When you want to use a JButton in your program you will usually start by reading the JButton documentation to learn how to create JButtons and to learn what operations can be performed on JButton objects. However, to discover all that a JButton can do you must read the documentation for its direct base class AbstractButton, and in turn read the documentation for JComponent, Container, Component, and Object as well. Fortunately, the Java platform documentation takes steps to make this process relatively painless by providing a section in each class detail page that lists the class’s inherited methods. Figure 5-8 shows a partial listing of the inherited methods for the JButton class.

image from book
Figure 5-7: JButton Inheritance Hierarchy

image from book
Figure 5-8: JButton Inherited Methods Grouped by Base Class (partial listing)

The inherited methods section is nice because you can quickly scan the list of inherited methods to see if the functionality you require is already provided for you. For example, suppose you wanted to hide a JButton. You would look first for a hide() method in the JButton class and, guess what? — it’s not there. The next step would be to examine the inherited methods section of the JButton class documentation where you would find both the hide() and the show() methods in the java.awt.Component class. You click on the hide() method link and it takes you to the hide() method documentation of the Component class where you learn that the hide() method is deprecated as of Java version 1.1 and that you should use the setVisible() method instead. The setVisible() method description is shown in figure 5-9.

image from book
Figure 5-9: setVisible() Function Description

You will repeat this process over and over when you write Java programs. At first you will be driven to the brink of insanity by the need to look up every class and its associated methods, but, with practice, you will begin to remember what classes provide what functionality and in what package those classes are located.

Fortunately, as I said earlier, you will only need to use a small sampling of the many classes supplied by the Java platform API and the functionality they provide to write useful Java programs. As your knowledge of Java programming steadily grows, so too will your knowledge of the Java platform API.

Deprecated Methods

In the JButton example above the hide() and show() methods were tagged as being deprecated. I’d like to take a moment here to explain what that means. The word deprecated, as it applies to systems and software, means to gradually phase out obsolete features in favor of new and improved features.

As the Java platform matures, methods that were supplied with older versions of the API are sometimes superseded by new methods that provide enhanced functionality. The old, superseded methods are then tagged as being deprecated. At some point in time these deprecated methods will no longer be supported by future versions of the Java platform. Although you can use a deprecated method in your code with no immediate ill effects, you’re best advised not to, especially if you are writing production code.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net