Chapter 11 Inheritance and Polymorphism
Section 11.2 Superclasses and Subclasses
1. Object-oriented programming allows you to derive new classes from existing classes. This is called .
a. encapsulation
b. inheritance
c. abstraction
d. generalization
#
2. Which of the following statements are true?
a. A subclass is a subset of a superclass.
b. A subclass is usually extended to contain more functions and more detailed information than its superclass.
c. “class A extends B” means A is a subclass of B.
d. “class A extends B” means B is a subclass of A.
#
66. Inheritance means .
a. that data fields should be declared private
b. that a class can extend another class
c. that a variable of supertype can refer to a subtype object
d. that a class can contain another class
#
Section 11.3 Using the super Keyword
Section 11.3.1 Calling Superclass Constructors
3. Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:
class Square extends GeometricObject {
double length;
Square(double length) {
GeometricObject(length);
}
}
a. The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the
length of the Square.
b. The program has a compile error because you attempted to invoke the GeometricObject class’s constructor illegally.
c. The program compiles fine, but it has a runtime error because of invoking the Square class’s constructor illegally.
#
4. Analyze the following code:
public class A extends B {
}
class B {
public B(String s) {
}