Page 543 - Introduction to Programming with Java: A Problem Solving Approach
P. 543

                13.2 The Object Class and Automatic Type Promotion 509
13.10 TheprotectedAccessModifier
13.11 GUITrack:Three-DimensionalGraphics(Optional)
13.1 Introduction
This is the second of two chapters on inheritance. The previous chapter applied a broad brush stroke to fundamental inheritance concepts. In this chapter, we narrow the focus and describe several inheritance- related topics in depth. We start with the Object class, which is the provided-by-Sun superclass of all other classes. We then discuss one of the cornerstones of object-oriented programming (OOP)—polymorphism. Polymorphism is the ability for a particular method call to perform different operations at different times. It occurs when you have a reference variable that refers to different types of objects during the course of a pro- gram’s execution. When the reference variable calls the polymorphic method, the reference variable’s object type determines which method is called at that time. Pretty cool, eh? Polymorphism provides programs with a great deal of power and versatility.
After introducing polymorphism, we describe its partner, dynamic binding. Dynamic binding is the mechanism used by Java to implement polymorphism. We then provide alternative implementations of polymorphism, using abstract classes and interfaces to make coding cleaner and even more versatile. We then describe the protected modifier, which simplifies access to inherited code. Finally, in an optional section, we present a three-dimensional graphics problem that illustrates polymorphism with the Java API.
 The material in this chapter is relatively difficult, but once you get it, you’ll truly understand what OOP is about, and you’ll know how to craft elegantly structured programs.
Apago PDF Enhancer
13.2 The Object Class and Automatic Type Promotion
The Object class is the ancestor of all other classes. It is the primordial ancestor—the root of the inheri- tance hierarchy. Any class that explicitly extends a superclass uses extends in its definition. Whenever anyone creates a new class that does not explicitly extend some other class, the compiler automatically makes it extend the Object class. Therefore, all classes eventually descend from the Object class. The Object class doesn’t have many methods, but the ones it has are significant, because they are always inher- ited by all other classes. In the next two sections you’ll see the Object class’s two most important methods, equals and toString. Since any class you write automatically includes these two methods, you need to be aware of what happens when these methods are called.
Before diving into the details of these two methods, however, we want to make you aware of a Java pro- cess that’s very similar to the numerical type promotion you studied in Chapter 3 and Chapter 11. There you saw that in the course of making an assignment or copying an argument into a parameter, the Java Virtual Machine (JVM) automatically promotes a numerical type—provided that the change conforms to a certain numerical hierarchy. For example, when an int value is assigned into a double variable, the JVM auto- matically promotes the int value to a double value.
An analogous automatic promotion also occurs with other types. When an assignment or argument passing operation involves different reference types, the JVM automatically promotes the source reference type to the target reference type if the target reference type is above the source reference type in the inheri- tance hierarchy. In particular, since the Object class is an ancestor of every other class, when the need arises, Java automatically promotes any class type to the Object type. The next section describes a situa- tion that stimulates this kind of type promotion.
 






















































































   541   542   543   544   545