You are on page 1of 196
Inheritance 7.1 INHERITANCE BASICS 416 Programming Example: A Person Class 417 Derived Classes 418 Overriding Method Definitions 421 Overriding Versus Overtoading 422 The final Modifier 422 Gotcha: Use of Private Instance Variables from the Base Class 423 Programming Tip: Assume That Your Coworkers Are Malicious 424 Gotcha: Private Methods Are Not Inherited 424 UML Inheritance Diagrams 424 7.2 PROGRAMMING WITH INHERITANCE 427 Constructors in Derived Classes 427 The thi s Method (Optional) 428 Call to an Overridden Method 429 Programming Example: Multilevel Derived Classes 430 Chapter Summary 465 "Answers to Salt-Test Quastons 466 Programming Projects 471 A Subtle Point About Overloading and Overriding (Optional) 435, Java Tip: You Cannot Use Multiple supers 435 Programming Tip: An Object Can Have More than ‘One Type 436, Programming Tip: “Is a” and “Has a” Relations 439 The Class Object 440 Case Study: Character Graphics 442, Abstract Classes 452 Interfaces (Optiona) 456 7.3 DYNAMIC BINDING AND POLYMORPHISM 458 Dynamic Binding 458 ‘Type Checking and Dynamic Binding 460 Dynamic Binding with toString 461 Polymorphism 462 Java Tip: ABetter equals Method (Optional) 463 inheritance 416 — ‘Like mother, like daughter.- Common saying Ths chapter covers inheritance, one of the key concepts in object-oriented program- ‘ming and one that is needed in order to use many of the libraries that come with the Java programming language. Inheritance will allow you to use an existing class to define new classes, making it easier to reuse software. OBJECTIVES Become famitiar with inheritance in general. Learn how to define and use derived classes in Java. Learn about dynamic binding and polymorphism in general and in Java. PREREQUISITES. You need to have read the material in Chapters | through 5 before you can understand this. chapter. Chapter 6 is not needed for this chapter. RAF INHERITANCE BASICS All men are mortal ‘Socrates is a man. ‘Therefore Socrates is mortal. Typical syllogism Inheritance allows you to define a very general class and then later define more special- ized classes simply by adding some new details to the older, more general class definition. This saves work, because the more specialized class inherits all the properties of the gen- eral class and you, the programmer, need only program the new features. For example, you might define a class for vehicles that has instance variables to record the vehicle’s number of wheels and maximum number of occupants. You might then define a class for automobiles and let the automobile class inherit all the instance variables, and methods of the class for vehicles. The class for automobiles would have added instance variables for such things as the amount of fuel in the fuel tank and the license plate number, and would also have some added methods. (Some vehicles, such as a horse and wagon, have no fuel tank and normally no license plate, but an automobile is a vehicle that has these “added” items.) You would have to describe the added instance variables and added methods, but if you used Java’s inheritance mechanism, you would get the instance variables and methods from the vehicle class automatically. Before we construct an example of inheritance within Java, we first need to set the stage. We'll do so with the following Programming Example.