Who was the inventor of Java?
Your answer: James Gosling
true
Q1 May 1
Consider classes Manager and Employee. Which should be the superclass and which should be the subclass?
Employee is the superclass and Manager is the subclass
Manager is the superclass and Employee is the subclass
Neither can be a subclass of the other.
Something else
Self Check 9.2
What are the inheritance relationships between classes BankAccount, CheckingAccount, and SavingsAccount?
Answer:CheckingAccount and SavingsAccount both inherit
from the more general class BankAccount.
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.
Car, Sedan, SUV
Vehicle, Motorcylce, Truck, Sedan, SUV (only Car class objects can be passed)
Vehicle, Motorcylce, Truck
Something else
Implementing Subclasses
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.
To get a ChoiceQuestion class, implement it as a subclass of Question
Specify what makes the subclass different from its superclass.
Subclass objects automatically have the instance variables that are declared in the superclass.
Only declare instance variables that are not part of the superclass objects.
A subclass inherits all methods that it does not override.
The ChoiceQuestion Class is a Subclass of the Question Class
Implementing Subclasses
The subclass inherits all public methods from the superclass
You declare any methods that are new to the subclass
You change the implementation of inherited methods if the inherited behavior is not appropriate.
Override a method: supply a new implementation for an inherited method
Implementing Subclasses
A ChoiceQuestion object differs from a Question object in three ways
Its objects store the various choices for the answer.
There is a method for adding answer choices.
The display method of the ChoiceQuestion class shows these choices so that the respondent can choose one of them.
Implementing Subclasses
The ChoiceQuestion class needs to spell out the three differences
public class ChoiceQuestion extends Question
{
// This instance variable is added to the subclass
private ArrayList<String> choices;
// This method is added to the subclass
public void addChoice(String choice, boolean correct) { . . . }
// This method overrides a method from the superclass
public void display() { . . . }
}
The extends reserved
word indicates that a
class inherits from a
superclass.
Implementing Subclasses
UML of ChoiceQuestion and Question
The ChoiceQuestion Class Adds an Instance Variable and a Method, and Overrides a Method
Syntax 9.1 Subclass Declaration
Implementing Subclasses
A ChoiceQuestion object
You can call the inherited methods on a subclass object
choiceQuestion.setAnswer("2");
The private instance variables of the superclass are inaccessible
The ChoiceQuestion methods cannot directly access the instance variable answer
ChoiceQuestion methods must use the public interface of the Question class to access its private data
Implementing Subclasses
Adding a new method: addChoice
public void addChoice(String choice, boolean correct)
{
choices.add(choice);
if (correct)
{
// Convert choices.size() to string
String choiceString = "" + choices.size();
setAnswer(choiceString);
}
}
addChoice method can not just access the answer variable in the superclass
It must use the setAnswer method
Invoke setAnswer on the implicit parameter
setAnswer(choiceString);
OR
this.setAnswer(choiceString);
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)
Answer:
a, b, d
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.
Answer:
public class Manager extends Employee
{
private double bonus;
// Constructors and methods omitted
}
Self Check 9.8
Which instance variables does the Manager class from Self Check 7 have?
Answer:name, baseSalary, and bonus
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.
Answer:
public class Manager extends Employee
{
. . .
public double getSalary() { . . . }
}
Self Check 9.10
Which methods does the Manager class from Self Check 9 inherit?
Answer:getName, setName, setBaseSalary
Common Error: Replicating Instance Variables from the Superclass
A subclass has no access to the private instance variables of the superclass:
public ChoiceQuestion(String questionText)
{
text = questionText; // Error—tries to access private superclass variable
}
Beginner's error: "solve" this problem by adding another instance variable with same name
Error!
public class ChoiceQuestion extends Question
{
private ArrayList<String> choices;
private String text; // Don’t!
. . .
}
The constructor compiles, but it doesn’t set the correct text!
the ChoiceQuestion
constructor should call the setText method of the Question class
Overriding Methods
If you are not satisfied with
the behavior of an inherited method,
you override it by specifying a new implementation
in the subclass.
An overriding
method can extend
or replace the
functionality of the
superclass method.
The display method of the ChoiceQuestion class needs to:
Display the question text.
Display the answer choices.
Overriding Methods
Problem: ChoiceQuestion's display method can’t access the text variable of the superclass directly because it is private.
Solution: It can call the display method of the superclass, by using the reserved
word super
public void display()
{
// Display the question text
super.display(); // OK
// Display the answer choices
. . .
}
super is a reserved word that forces execution of the superclass method
What was the original name of the Java language?
1: *7
2: Duke
3: Oak
4: Gosling
Your answer: *7
false
In which country was the inventor of Java born?
1: Australia
2: Canada
3: Denmark
4: United States
Your answer: 2
true
Syntax 9.2 Calling a Superclass 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));
}
}
}
Answer:
The method is not allowed to access the
instance variable text from the superclass
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));
}
}
}
Answer:
The type of the this reference is ChoiceQuestion.
Therefore, the display method of ChoiceQuestion is selected, and the method calls itself
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?
Answer:
Because there is no ambiguity. The subclass
doesn’t have a setAnswer method.
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).
Answer:
public String getName()
{
return "*" + super.getName();
}
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.
Answer:
public double getSalary()
{
return super.getSalary() + bonus;
}
Common Error: Accidental Overloading
Overloading: when two methods have the same name but different parameter types
Overriding: when a subclass method provides an implementation of a superclass method whose parameter variables have the same types.
When overriding a method, the types of the parameter variables must match exactly.
Syntax 9.3 Constructor with Superclass Initializer
Polymorphism
Problem: to present both Question and ChoiceQuestion with the same program
We do not need to know the exact type of the question
We need to display the question
We need to check whether the user supplied the correct answer
The Question superclass has methods for displaying and checking.
We can simply declare the parameter variable of the presentQuestion method to have the type Question
public static void presentQuestion(Question q)
{
q.display();
System.out.print("Your answer: ");
Scanner in = new Scanner(System.in);
String response = in.nextLine();
System.out.println(q.checkAnswer(response));
}
Polymorphism - continued
We can substitute a subclass object whenever a superclass object is expected
ChoiceQuestion second = new ChoiceQuestion();
. . .
presentQuestion(second); // OK to pass a ChoiceQuestion
When the presentQuestion method executes -
The object references stored in second and
q refer to the same object
The object is of type ChoiceQuestion
Variables of Different
Types Referring to the
Same Object
Polymorphism - continued
The variable q knows less than the full story about the object to which it refers
A Question Reference
Can Refer to an Object
of Any Subclass
of Question
In the same way that vehicles can
differ in their method of locomotion,
polymorphic objects carry out tasks
in different ways.
Polymorphism - continued
When the virtual
machine calls an
instance method -
It locates the
method of the
implicit parameter’s class.
This is called
dynamic method
lookup
Dynamic method lookup allows us to treat objects of different classes in a uniform way.
This feature is called polymorphism.
We ask multiple objects to carry out a task, and each object does so in its own way.
Polymorphism
means “having multiple
forms”
It allows
us to manipulate
objects that share
a set of tasks, even though the tasks are
executed in
different ways.
Who was the inventor of Java?
Your answer: Bjarne Stroustrup
false
In which country was the inventor of Java born?
1: Australia
2: Canada
3: Denmark
4: United States
Your answer: 2
true
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;
Answer:
a only.
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?
Answer:
It belongs to the class BankAccount or one of its
subclasses.
Self Check 9.18
Declare an array quiz that can hold a mixture of Question and ChoiceQuestion objects.
Answer:
Question[] quiz = new Question[SIZE];
Self Check 9.19
Consider the code fragment
ChoiceQuestion cq = . . .; // A non-null value
cq.display();
Which actual method is being called?
Answer: You cannot tell from the fragment—cq may
be initialized with an object of a subclass
of
ChoiceQuestion. The display method of whatever
object cq references is invoked.
Self Check 9.20
Is the method call Math.sqrt(2) resolved through dynamic method lookup?
Answer: No. This is a static method of the Math class.
There is no implicit parameter object that
could be used to dynamically look up a
method.
Object: The Cosmic Superclass
Every class defined without an explicit extends clause automatically
extend Object:
The class Object is the direct or indirect superclass of every class in Java
Somemethods defined in Object:
toString
- which yields a string describing the object
equals
- which compares objects with each other
hashCode - which yields a numerical code for storing the object in a set
Object: The Cosmic Superclass
The Object Class Is the Superclass of Every Java Class
Overriding the toString Method
Returns a string representation of the object
Useful for debugging:
Rectangle box = new Rectangle(5, 10, 20, 30);
String s = box.toString();
// Sets s to "java.awt.Rectangle[x=5,y=10,width=20,height=30]"
toString is called whenever you concatenate a string with an object: