Introducing Classes and More in Java - Organizing Classes and Class Behavior
(Page 3 of 7 )
An introduction to object-oriented programming in Java isn't complete without looking at three more concepts: inheritance, interfaces, and packages.
These three things are all mechanisms for organizing classes and class behavior. The Java class library uses these concepts, and the classes you create for your own programs also need them.
Inheritance
Inheritance is one of the most crucial concepts in object-oriented programming, and it has a direct effect on how you design and write your own Java classes.
Inheritance is a mechanism that enables one class to inherit all the behavior and attributes of another class.
Through inheritance, a class immediately has all the functionality of an existing class. Because of this, you only must define how the new class is different from an existing class.
With inheritance, all classes—those you create and those from the Java class library and other libraries—are arranged in a strict hierarchy.
A class that inherits from another class is called a subclass. The class that gives the inheritance is called a superclass.
A class can have only one superclass, but each class can have an unlimited number of subclasses. Subclasses inherit all the attributes and behavior of their superclasses.
In practical terms, this means that if the superclass has behavior and attributes that your class needs, you don't have to redefine it or copy that code to have the same behavior and attributes. Your class automatically receives these things from its superclass, the superclass gets them from its superclass, and so on, all the way up the hierarchy. Your class becomes a combination of its own features and all the features of the classes above it in the hierarchy.
The situation is comparable to the way you inherited all kinds of things from your parents, such as your height, hair color, love of grunge music, and reluctance to ask for directions. They inherited some of these things from their parents, who inherited from theirs, and backward through time to the Garden of Eden, Big Bang, or insert personal cosmological belief here.
Figure 1.2 shows the way a hierarchy of classes is arranged.

Figure 1.2 A class hierarchy.
At the top of the Java class hierarchy is the class Object—all classes inherit from this one superclass. Object is the most general class in the hierarchy, and it defines behavior inherited by all the classes in the Java class library. Each class farther down the hierarchy becomes more tailored to a specific purpose. A class hierarchy defines abstract concepts at the top of the hierarchy. Those concepts become more concrete farther down the line of subclasses.
Often when you create a new class in Java, you will want all the functionality of an existing class with some modifications of your own creation. For example, you might want a version of a CommandButton that makes a sound when clicked.
To receive all the CommandButton functionality without doing any work to re-create it, you can define your class as a subclass of CommandButton. Your class then would automatically inherit behavior and attributes defined in CommandButton, as well as the behavior and attributes defined in the superclasses of CommandButton. All you have to worry about are the things that make your new class different from CommandButton itself. Subclassing is the mechanism for defining new classes as the differences between those classes and their superclass.
Subclassing is the creation of a new class that inherits from an existing class. The only task in the subclass is to indicate the differences in behavior and attributes between itself and its superclass.
If your class defines an entirely new behavior and isn't a subclass of another class, you can inherit directly from the Object class. This allows it to fit neatly into the Java class hierarchy. In fact, if you create a class definition that doesn't indicate a superclass, Java assumes that the new class is inheriting directly from Object. The VolcanoRobot class you created earlier inherited from the Object class.
Next: Creating a Class Hierarchy >>
More Java Articles
More By Sams Publishing
|
This article is excerpted from chapter one of Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Buy this book now.
|
|