Chapter8 TB
Chapter8 TB
Chapter 8
Polymorphism and Abstract Classes
◼ Multiple Choice
1) The principals of object oriented programming include:
(a) encapsulation
(b) inheritance
(c) polymorphism
(d) all of the above
Answer: D
2) ____________ refers to the process of associating a method definition with a method invocation.
(a) Binding
(b) Encapsulation
(c) Inheritance
(d) Polymorphism
Answer: A
3) __________ binding refers to the method definition being associated with the method invocation
when the code is compiled.
(a) Dynamic
(b) Late
(c) Early
(d) None of the above
Answer: C
4) __________ refers to the ability to associate many meanings to one method name by means of the
late binding mechanism.
(a) Inheritance
(b) Encapsulation
(c) Polymorphism
(d) None of the above
Answer: C
Chapter 8 Polymorphism and Abstract Classes 2
6) Java does not use late binding for methods marked as:
(a) final
(b) static
(c) private
(d) all of the above
Answer: D
10) If you choose to use the method clone in your code, you must ___________ the clone method.
(a) overload
(b) encapsulate
(c) override
(d) protect
Answer: C
Chapter 8 Polymorphism and Abstract Classes 3
14) A class that has at least one abstract method is called an:
(a) concrete class
(b) encapsulated class
(c) abstract class
(d) private class
Answer: C
16) An abstract class must have the modifier ___________ included in the class heading.
(a) static
(b) abstract
(c) final
(d) private
Answer: B
Chapter 8 Polymorphism and Abstract Classes 4
◼ True/False
1) Polymorphism refers to the ability to associate many meanings to one method through dynamic
binding.
Answer: True
3) Late binding refers to the method definition being associated with the method invocation when the
method is invoked at run time.
Answer: True
5) The final modifier is included before the definition of the method, then the method can be redefined
in a derived class.
Answer: False
6) Java uses late binding with private methods, methods marked final, or static methods.
Answer: False
7) The type of the variable naming an object determines which method names can be used in an
invocation with that calling object.
Answer: True
9) The method clone has one parameter and should return a copy of the calling object.
Answer: False
10) An abstract class is a class that has some methods without complete definitions.
Answer: True
11) An abstract method serves as a placeholder for a method that must be defined in all derived classes.
Answer: True
Chapter 8 Polymorphism and Abstract Classes 5
◼ Short Answer/Essay
1) Explain the difference between early and late binding.
Answer: Early binding occurs when the method definition is associated with the method invocation
when the code is compiled. Late binding occurs when the method definition is associated with the
method invocation at run time.
mo = (MyObject) someObject;
else
Answer: The semi-colon is missing from the end of the method definition. The correct definition
follows:
9) Draw an inheritance hierarchy to represent a shoe object. The base class should have derived
classes of Dress Shoes, Tennis Shoes and Boots.
Answer:
java.lang.Object
Shoe
10) Implement the base class in the shoe hierarchy in number 8 above.
Answer:
public Shoe()
color = "";
designer = "";
size = 0;
setColor(c);
setDesigner(d);
setSize(s);
color = c;
designer = d;
if(s > 0)
size = s;
return color;
return designer;
return size;
Chapter 8 Polymorphism and Abstract Classes 9
11) Derive a class named Dress Shoes from the base class created in number 9 above.
Answer:
private String type; //valid types include pumps, heels and flats
public DressShoe()
super();
type = "";
super(c, d, s);
setType(t);
type = t;
Chapter 8 Polymorphism and Abstract Classes 10
return type;
if(o == null)
return false;
return false;
else
{
Chapter 8 Polymorphism and Abstract Classes 11
getColor().equals(otherDressShoe.getColor()) &&
type.equals(otherDressShoe.type));
12) Derive a class named Tennis Shoes from the base class created in number 9 above.
Answer:
public TennisShoe()
super();
soleType = "";
canvasType = "";
Chapter 8 Polymorphism and Abstract Classes 12
super(c, d, s);
setSoleType(st);
setCanvasType(ct);
soleType = st;
canvasType = ct;
return soleType;
}
Chapter 8 Polymorphism and Abstract Classes 13
return canvasType;
if(o == null)
return false;
return false;
else
Chapter 8 Polymorphism and Abstract Classes 14
return
(getDesigner().equals(otherTennisShoe.getDesigner())
&&
getColor().equals(otherTennisShoe.getColor()) &&
soleType.equals(otherTennisShoe.soleType) &&
canvasType.equals(otherTennisShoe.canvasType));
13) Derive a class named Boots from the base class created in number 9 above.
Answer:
private String heelType; //valid types include pumps, heels and flats
public Boot()
{
Chapter 8 Polymorphism and Abstract Classes 15
super();
heelType = "";
super(c, d, s);
setHeelType(ht);
heelType = ht;
return heelType;
}
Chapter 8 Polymorphism and Abstract Classes 16
if(o == null)
return false;
return false;
else
getColor().equals(otherBoot.getColor()) &&
Chapter 8 Polymorphism and Abstract Classes 17
heelType.equals(otherBoot.heelType));
14) Override the clone method inherited in the Dress Shoes class created in number 10 above.
Answer:
this.getType());
15) Override the clone method inherited in the Tennis Shoes class created in number 11 above.
Answer:
this.getSoleType(), this.getCanvasType());
16) Override the clone method inherited in the Boots class created in number 12 above.
Answer:
this.getHeelType());