Chapter 9 - Inheritance
Which of the following indicates that a class named ClassA class is a superclass of the ClassB class?
public class ClassB extends ClassA
You are creating a Motorcycle class which is supposed to inherit from the Vehicle class. Which of the following class declaration statements will accomplish this?
public class Motorcycle extends Vehicle
A subclass of Value, LargerValue, is defined with a getValue method that returns twice the value of the parent. Which line is the body of LargerValue's getValue method?
return super.getValue() * 2;
5. Insert the missing code in the following code fragment. This fragment is intended to call the Vehicle class's method.
setVehicleClass(2.0);
Which reserved word must be used to call a method of a superclass?
super
Complete the code in the Auto class constructor to store the type data.
super(type);
To test whether an object belongs to a particular type, use ___.
the instanceof operator.
If a subclass uses the same method name but different parameter types for a method that appears in its superclass, ____.
the subclass method has overloaded its superclass's method
If a subclass defines the same method name and the same parameter types for a method that appears in its superclass, ____.
the subclass method overrides the superclass method
6. Insert the missing code in the following code fragment. This fragment is intended to call the Vessel class's method.
vesselLength(26.0);
public class Auto extends Vehicle { . . . public Auto(int numberAxles) { super(numberAxles); } } What does this code do?
It invokes the constructor of the Vehicle class from within the constructor of the Auto class.
A class that represents the most general entity in an inheritance hierarchy is called a/an ____.
Superclass
super(numberAxles); //line #1 If the line marked "//line #1" was missing, which of these statements would be correct?
The Motorcycle class constructor would invoke the constructor of the Vehicle class with no parameters.
Which of the following statements is correct? (changed wording)
The Motorcycle class's setVehicleClass method overrides the Vehicle class's setVehicleClass method.
public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Motorcycle extends Vehicle { . . . public void setModelName(String model) { . . . } public void setVehicleClass(double numberAxles) { . . . } } Which of the following statements is NOT correct?
The Vehicle class can call the setModelName method.
Which of the following statements about comparing objects is correct?
The equals method is used to compare whether two objects have the same contents.
Consider the following code snippet: Vehicle aVehicle = new Auto(); aVehicle.moveForward(200);
The moveForward method of the Auto class will be executed.
You are creating a class inheritance hierarchy about motor vehicles that will contain classes named Vehicle, Auto, and Motorcycle. Which of the following statements is correct?
Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses.
Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); Assume that the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type. The process for determining which class's moveForward method to execute is called ____.
dynamic lookup.
To create a subclass, use the ____ keyword
extends
The ____ reserved word in a class definition ensures that subclasses cannot be created from this class
final
The ____ reserved word in a method definition ensures that subclasses cannot override this method.
final
Complete the code in this program snippet to correctly display the auto's type.
myAuto.displayInfo()
ExcitedMessage greeting = new ExcitedMessage("Hello"); System.out.print(greeting.getMessage()); // will print "Hello!!" Which ExcitedMessage constructor will give this behavior?
public ExcitedMessage(String line) { super(line + "!!"); }
Which of the following indicates that a class named Class1 is a subclass of a class named Class2?
public class Class1 extends Class2
What is the output of the following lines of code? Child kid = new Child(-14); Parent adult = new Parent(); System.out.println(kid.getValue() + " " + adult.getValue());
100 100
What is the output of the following lines of code? Child kid = new Child(-14); Parent kid2 = new Child(21); System.out.println(kid.getValue() + " " + kid2.getValue());
100 100
If a Motorcycle class is created as a subclass of the Vehicle class, which of the following statements is correct?
A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
If a Speedboat class is created as a subclass of the Vessel class, which of the following statements is correct?
A Speedboat object inherits and can directly use the method setVesselClass but cannot directly use the instance variable manufacturer.
Which of the following statements about superclasses and subclasses is true?
A subclass extends a superclass.
Which of the following is true regarding subclasses?
A subclass inherits methods and instance variables from its superclass.
Which of the following is true regarding inheritance?
A superclass can force a programmer to override a method in any subclass created from it.
A class that cannot be instantiated is called a/an ____.
Abstract class.
Which of the following statements about abstract methods is true
An abstract method has a name, parameters, and a return type, but no code in the body of the method.
A class that represents a more specific entity in an inheritance hierarchy is called a/an ____.
Subclass
Which lines in the following code will compile without error? Parent kid = new Child(); kid.doSomething(); // line 1 kid.doAnotherThing(); // line 2
Line 1 only
Which lines in the following code will compile without error? Child kid = new Child(); kid.doSomething(); // line 1 kid.doAnotherThing(); // line 2
Lines 1 and 2
13. All rodents are mammals and all canines are mammals. No canines are rodents and no rodents are canines. What hierarchy best captures this info?
Mammal is a superclass of Rodent and Mammal
12. All hamsters are rodents and all rodents are mammals. What hierarchy best captures this information?
Mammal is a superclass of Rodent and Rodent is a superclass of Hamster
To override a superclass method in a subclass, the subclass method ____.
Must use the same method name and the same parameter types.
When declared as protected, data in an object can be accessed by ____.
Only by that class's methods, by all of its subclasses, and by methods in classes within the same package.
public class Vehicle ... public void setVehicleAtrributes() ... public class Auto extends Vehicle ... public void setVehicleAtrributes() ... Which of the following statements is correct?
The subclass is overriding a superclass method.
What must a subclass do to modify a private superclass instance variable?
The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable.
public void deposit(double amount) { transactionCount ++; super.deposit(amount); }
This method calls a public method in its superclass.
public void deposit(double amount) { transactionCount ++; deposit(amount); } Which of the following statements is true?
This method will call itself.
Which of the following statements about inheritance is correct?
You can always use a subclass object in place of a superclass object.
If a class has an abstract method, which of the following statements is NOT true?
You can construct an object from this class.
Which of the following statements about classes is true?
You can create an object from a class declared with the keyword final
Which of the following statements about classes is true?
You can create an object from a concrete class, but not from an abstract class.
A class from which you cannot create objects is called a/an ____.
abstract class.
If a subclass contains a method with the same name as a method in its superclass, but with different parameter types, the subclass method is said to ____ the method of the superclass.
overload
To ensure that an instance variable can only be accessed by the class that declared it, the variable should be declared as ____.
private
With a few exceptions, instance variables of classes should always have ___ access.
private