Chapter8

advertisement
Chapter 8
Polymorphism and Abstract Classes
1
 Multiple Choice
1)
2)
3)
4)
5)
6)
7)
8)
9)
The principals of object oriented programming include:
encapsulation
inheritance
polymorphism
all of the above
____________ refers to the process of associating a method definition with a method invocation.
Binding
Encapsulation
Inheritance
Polymorphism
__________ binding refers to the method definition being associated with the method invocation
when the code is compiled.
Dynamic
Late
Early
None of the above
__________ refers to the ability to associate many meanings to one method name by means of the
late binding mechanism.
Inheritance
Encapsulation
Polymorphism
None of the above
A method marked as final means the compiler uses ________ binding.
dynamic
early
late
none of the above
Java does not use late binding for methods marked as:
final
static
private
all of the above
Assigning an object of a derived class to a variable of a base class is called:
static binding
dynamic binding
upcasting
downcasting
Assigning an object of an ancestor class to a descendent class is called:
static binding
dynamic binding
upcasting
downcasting
The clone method has ________ parameters.
zero
one
10)
11)
12)
13)
14)
15)
16)

1)
2)
3)
4)
two
three
If you choose to use the method clone in your code, you must ___________ the clone method.
overload
encapsulate
override
protect
The clone method return type is:
the same as the cloned object
Object
String
none of the above
You cannot create an object using a/an:
superclass constructor
subclass constructor
ancestor class constructor
abstract class constructor
An abstract method cannot be modified by:
public
protected
private
none of the above
A class that has at least one abstract method is called an:
concrete class
encapsulated class
abstract class
private class
A class with no abstract methods is called a
concrete class
encapsulated class
abstract class
private class
An abstract class must have the modifier ___________ included in the class heading.
static
abstract
final
private
True/False
Polymorphism refers to the ability to associate many meanings to one method through dynamic
binding.
Java allows an instance of an abstract class to be instantiated.
Late binding refers to the method definition being associated with the method invocation when the
method is invoked at run time.
Early binding enables the compiler to be more efficient.
5)
6)
7)
8)
9)
10)
11)

1)
2)
3)
4)
5)
6)
7)
The final modifier is included before the definition of the method, then the method can be redefined
in a derived class.
Java uses late binding with private methods, methods marked final, or static methods.
The type of the variable naming an object determines which method names can be used in an
invocation with that calling object.
Downcasting should be used only in situations where it makes sense.
The method clone has one parameter and should return a copy of the calling object.
An abstract class is a class that has some methods without complete definitions.
An abstract method serves as a placeholder for a method that must be defined in all derived classes.
Short Answer/Essay
Explain the difference between early and late binding.
What is polymorphism and how does it relate to late binding?
What are the advantages of polymorphism?
Write a decision statement to determine if an object should be downcast.
Describe the limitations of the copy constructor.
What is an abstract method?
What is wrong with the following method definition?
public abstract void doSomething(int count)
8)
9)
10)
11)
12)
13)
Why should the instanceOf operator be used in conjunction with downcasting?
Draw an inheritance hierarchy to represent a shoe object. The base class should have derived
classes of Dress Shoes, Tennis Shoes and Boots.
Implement the base class in the shoe hierarchy in number 9 above.
Derive a class named Dress Shoes from the base class created in number 10 above.
Derive a class named Tennis Shoes from the base class created in number 10 above.
Derive a class named Boots from the base class created in number 10 above.
private String heelType;
14)
15)
16)
//valid types include pumps, heels and flats
Override the clone method inherited in the Dress Shoes class created in number 11above.
Override the clone method inherited in the Tennis Shoes class created in number 12 above.
Override the clone method inherited in the Boots class created in number 13 above.
Download