Inheritance In java


Inheriting Methods from Other Classes
This might be a surprise to you, but your Java objects are ideally suited for childbearing. When you create a program as an object--a set of attributes and behavior--you have designed something that's ready to pass these qualities on to offspring. Like most offspring, these child objects will take on a lot of the attributes and behavior of their parent. They also can do some things differently than their parent does and can add some extra attributes and behavior that pop is incapable of.
This system is called inheritance, and it's something every superclass parent gives to its subclass children. Inheritance is one of the most useful aspects of object-oriented programming, and you'll be learning more about it during this hour.
The following topics will be covered:
·         Superclasses and subclasses
·         An inheritance hierarchy
·         Overriding methods
·         Creating a subclass
·         Positioning behavior
The Power of Inheritance
Without knowing it, you have used inheritance every time you used one of the standard Java classes such as String or Math. Java classes are organized into a pyramid-shaped hierarchy of classes in which all classes descend from the Object class.
A class of objects inherits from all superclasses that are above it. To get a working idea of how this works, look at the Applet class. This class is the superclass of all applets, which are Java programs that you will write for the World Wide Web. The family tree of Applet is shown in Figure 12.1. Each of the boxes is a class, and the lines connect a superclass above to any subclasses that it has below.
Figure 12.1. <../art/12/12tja01.jpg> The family tree of the Applet class.
At the top is the Object class. Applet has four superclasses above it in the hierarchy: Panel, Container, Component, and Object. The Applet class inherits attributes and behavior from each of these classes because each is directly above it in the hierarchy of superclasses. Applet does not inherit anything from the five shaded classes in Figure 12.1, which include Dialog and Frame, because they are not above it in the hierarchy.
If this seems confusing, think of the hierarchy as a family tree. Applet will inherit from its parents, their parents, and on upward. It even might inherit some things from its great- great-grandparent Object. The Applet class won't inherit from its siblings or its cousins, however.
Setting up a complicated hierarchy of classes is a difficult thing, but it makes it easier to create new programs later on. The amount of work you need to do to write a new class of objects is reduced. Creating a new class boils down to the following task: You only have to define the ways in which it is different from an existing class. The rest of the work is done for you.
As an example, consider the popular video game Tetris. It has been adapted for dozens of different operating systems, processors, and programming languages since being written by Soviet mathematician Alexey Pajitnov and has been created as a Java class by several programmers. In case you somehow avoided Tetris during the past decade by lapsing into a coma or falling into a deep meditative trance, the game works as follows: Blocks of different shapes fall from the top of the screen, and you must organize them into unbroken horizontal lines before they stack up too high.
The Java source file for several adaptations of Tetris is available for your use. If you wanted to create a new version of Tetris based on one of these existing classes, you could make your game a subclass of an existing Tetris game. All you would have to do is create the things that are new or different about your version, and you'd end up with a new game.
Inheriting Behavior and Attributes
The behaviors and attributes of a class are a combination of two things: its own behavior and attributes and all behavior and attributes it inherited from its superclasses.
The following are some of the behavior and attributes of Applet:
·         The equals() method determines whether an Applet object has the same value as another object.
·         The setBackground()method sets the background color displayed on the applet window.
·         n The add()method adds user interface components such as buttons and text fields to the applet.
·         The showStatus()method displays a line of text in a Web browser's status bar.
The Applet class can use all of these methods, even though showStatus() is the only one it didn't inherit from another class. The equals() method is defined in Object, setBackground() comes from Component, and add() comes from Container.
Overriding Methods
Some of the methods defined in the Applet class of objects also were defined in one of its superclasses. As an example, the resize() method is set up in the Applet class and the Component class. This method calls on the Web browser displaying the applet to resize the applet's display area. When a method is defined in a subclass and its superclass, the subclass method is used. This enables a subclass to change, replace, or completely wipe out some of the behavior or attributes of its superclasses.
Creating a new method in a subclass to change behavior inherited from a superclass is called overriding the method. You need to override a method any time the inherited behavior will produce an undesired result.
Establishing Inheritance
You establish a class as the subclass of another class with the extends statement, as in the following:
class AnimatedLogo extends java.applet.Applet {
    // program goes here
}
This statement establishes the AnimatedLogo class of objects as a subclass of Applet, using the full class name of java.applet.Applet. As you will see during the next hour, all applets in Java must be subclasses of Applet because they need the functionality this class provides in order to run on a World Wide Web page.
One method that AnimatedLogo will have to override is the paint() method, which is used to redraw all things that are shown on the program's display area. The paint() method is implemented by the Component class and is passed all the way down to AnimatedLogo. However, the paint() method does not do anything. It exists so that subclasses of Component have a method they can use when the display must be redrawn.
To override a method, you must start the method in the same way it started in the superclass it was inherited from. A public method must remain public, the value sent back by the method must be the same, and the number and type of arguments to the method must not change.
The paint() method of the Component class begins as follows:
public void paint(Graphics g) {
When AnimatedLogo overrides this method, it must begin with a statement like this:
public void paint(Graphics screen) {
The only difference is in the name of the Graphics object, which does not matter when determining if the methods are created in the same way. Because both paint() methods are public, return no value because of the void statement, and have a Graphics object as their only parameter, they match.

Post a Comment

Previous Post Next Post