Chapter 9 Inheritance and Polymorphism
1. (a) The printout is
A’s no-arg constructor is invoked
(b) The default constructor of B attempts to invoke the default of constructor of
A, but class A's default constructor is not defined.
2. All false.
(1) A subclass is an extension of a superclass and normally contains more details
information than its superclass.
(2) If a subclass’s constructor explicitly invoke a superclass’s constructor, the
superclass’s no-arg constructor is not invoked.
(3) You can only override accessible instance methods.
(4) You can only override accessible instance methods.
3. The following lines are erroneous:
{
radius = radius; // Must use this.radius = radius
}
class B extends Circle (missing extends)
{
Circle(radius); // Must use super(radius)
length = length; // Must use this.length = length
}
public double getArea()
{
return getArea()*length; // super.getArea()
}
4. Method overloading defines methods of the same name in a class. Method
overriding modifies the methods that are defined in the superclasses.
5. Yes, because these two methods are defined in the Object class; therefore, they
are available to all Java classes. The subclasses usually override these methods to
provide specific information for these methods.
The toString() method returns a string representation of the object; the equals()
method compares the contents of two objects to determine whether they are the
same.
6. B’s constructor is invoked