Big Java 4

Chapter 9 – Inheritance

Chapter Goals

vehicles

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

question hierarchy
Inheritance Hierarchy of Question Types

Example: Computer-graded quiz

section_1/Question.java

Your browser does not support this feature

section_1/QuestionDemo1.java

Your browser does not support this feature Program Run:

Q1 May 1

Consider classes Manager and Employee. Which should be the superclass and which should be the subclass?
  1. Employee is the superclass and Manager is the subclass
  2. Manager is the superclass and Employee is the subclass
  3. Neither can be a subclass of the other.
  4. Something else

Self Check 9.2

What are the inheritance relationships between classes BankAccount, CheckingAccount, and SavingsAccount?

Q2 May 1

Consider the method doSomething(Car c). List all vehicle classes from Figure 1 whose objects CAN NOT be passed to this method.
vehicle hierarcy

  1. Car, Sedan, SUV
  2. Vehicle, Motorcylce, Truck, Sedan, SUV (only Car class objects can be passed)
  3. Vehicle, Motorcylce, Truck
  4. Something else

Implementing Subclasses

stretch limo
Like the manufacturer of a stretch limo, who starts with a regular car and modifies it, a programmer makes a subclass by modifying another class.

Implementing Subclasses

Implementing Subclasses

A ChoiceQuestion object differs from a Question object in three ways

Implementing Subclasses

Implementing Subclasses

The ChoiceQuestion Class Adds an Instance Variable and a Method, and Overrides a Method

Syntax 9.1 Subclass Declaration

Subclass declaration

Implementing Subclasses

Implementing Subclasses

Q1 May 6

Suppose q is an object of the class Question and cq an object of the class ChoiceQuestion. Which of the following calls are legal?

a. q.setAnswer(response)
b. cq.setAnswer(response)
c. q.addChoice(choice, true)
d. cq.addChoice(choice, true)

Self Check 9.7

Suppose the class Employee is declared as follows:
public class Employee
{
   private String name;
   private double baseSalary;
   
   public void setName(String newName) { . . . }
   public void setBaseSalary(double newSalary) { . . . }
   public String getName() { . . . }
   public double getSalary() { . . . }
}
Declare a class Manager that inherits from the class Employee and adds an instance variable bonus for storing a salary bonus. Omit constructors and methods.

Self Check 9.8

Which instance variables does the Manager class from Self Check 7 have?

Self Check 9.9

In the Manager class, provide the method header (but not the implementation) for a method that overrides the getSalary method from the class Employee.

Self Check 9.10

Which methods does the Manager class from Self Check 9 inherit?

Common Error: Replicating Instance Variables from the Superclass

Overriding Methods

Overriding Methods

section_3/QuestionDemo2.java

Your browser does not support this feature

section_3/ChoiceQuestion.java

Your browser does not support this feature Program Run:

Syntax 9.2 Calling a Superclass Method

Calling a super class method

Self Check 9.11

What is wrong with the following implementation of the display method?
public class ChoiceQuestion
{
   . . .
   public void display()
   {
      System.out.println(text);
      for (int i = 0; i < choices.size(); i++)
      {
         int choiceNumber = i + 1;
         System.out.println(choiceNumber + ": " + choices.get(i));
      }
   }
}

Self Check 9.12

What is wrong with the following implementation of the display method?
public class ChoiceQuestion
{
   . . .
   public void display()
   {
      this.display();
      for (int i = 0; i < choices.size(); i++)
      {
         int choiceNumber = i + 1;
         System.out.println(choiceNumber + ": " + choices.get(i));
      }
   }
}

Self Check 9.13

Look again at the implementation of the addChoice method that calls the setAnswer method of the superclass. Why don’t you need to call super.setAnswer?

Self Check 9.14

In the Manager class of Self Check 7, override the getName method so that managers have a * before their name (such as *Lin, Sally).

Self Check 9.15

In the Manager class of Self Check 9, override the getSalary method so that it returns the sum of the salary and the bonus.

Common Error: Accidental Overloading

Syntax 9.3 Constructor with Superclass Initializer

supper class initializer of constructor

Polymorphism

Polymorphism - continued

Polymorphism - continued

Polymorphism - continued

section_4/QuestionDemo3.java

Your browser does not support this feature Program Run:

Self Check 9.16

Assuming SavingsAccount is a subclass of BankAccount, which of the following code fragments are valid in Java?

a. BankAccount account = new SavingsAccount();
b. SavingsAccount account2 = new BankAccount();
c. BankAccount account = null;
d. SavingsAccount account2 = account;

Self Check 9.17

If account is a variable of type BankAccount that holds a non-null reference, what do you know about the object to which account refers?

Self Check 9.18

Declare an array quiz that can hold a mixture of Question and ChoiceQuestion objects.

Self Check 9.19

Consider the code fragment
ChoiceQuestion cq = . . .; // A non-null value
cq.display();
Which actual method is being called?

Self Check 9.20

Is the method call Math.sqrt(2) resolved through dynamic method lookup?

Object: The Cosmic Superclass

Object: The Cosmic Superclass

The Object Class Is the Superclass of Every Java Class

Object hierarchy

Overriding the toString Method

Overriding the toString Method

Overriding the equals Method

Overriding the equals Method

Two References to Equal Objects
equal objects

Two References to the Same Object
same object

Overriding the equals Method

Overriding the equals Method

The instanceof Operator

Self Check 10.21

Why does the call
System.out.println(System.out);
produce a result such as java.io.PrintStream@7a84e4?

Self Check 10.22

Will the following code fragment compile? Will it run? If not, what error is reported?
Object obj = "Hello";
System.out.println(obj.length());

Self Check 9.23

Will the following code fragment compile? Will it run? If not, what error is reported?
Object obj = "Who was the inventor of Java?";
Question q = (Question) obj;
q.display();

Self Check 9.24

Why don’t we simply store all objects in variables of type Object?

Self Check 9.25

Assuming that x is an object reference, what is the value of x instanceof Object?