You are on page 1of 18

Chapter 8 Polymorphism and Abstract Classes 1

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

5) A method marked as final means the compiler uses ________ binding.


(a) dynamic
(b) early
(c) late
(d) none of the above
Answer: B

6) Java does not use late binding for methods marked as:
(a) final
(b) static
(c) private
(d) all of the above
Answer: D

7) Assigning an object of a derived class to a variable of a base class is called:


(a) static binding
(b) dynamic binding
(c) upcasting
(d) downcasting
Answer: C

8) Assigning an object of an ancestor class to a descendent class is called:


(a) static binding
(b) dynamic binding
(c) upcasting
(d) downcasting
Answer: D

9) The clone method has ________ parameters.


(a) zero
(b) one
(c) two
(d) three
Answer: A

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

11) The clone method return type is:


(a) the same as the cloned object
(b) Object
(c) String
(d) none of the above
Answer: B

12) You cannot create an object using a/an:


(a) superclass constructor
(b) subclass constructor
(c) ancestor class constructor
(d) abstract class constructor
Answer: D

13) An abstract method cannot be modified by:


(a) public
(b) protected
(c) private
(d) none of the above
Answer: C

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

15) A class with no abstract methods is called a


(a) concrete class
(b) encapsulated class
(c) abstract class
(d) private class
Answer: A

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

2) Java allows an instance of an abstract class to be instantiated.


Answer: False

3) Late binding refers to the method definition being associated with the method invocation when the
method is invoked at run time.
Answer: True

4) Early binding enables the compiler to be more efficient.


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

8) Downcasting should be used only in situations where it makes sense.


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.

2) What is polymorphism and how does it relate to late binding?


Answer: Polymorphism refers to the ability to associate multiple meanings to one method name by
means of the late binding mechanism. Polymorphism and late binding are technically the same
topic.

3) What are the advantages of polymorphism?


Answer: An advantage of polymorphism is that it allows the programmer to generalize on common
operations of the specific data type as opposed to having to define all the specific details.

4) Write a decision statement to determine if an object should be downcast.


Answer:

MyObject mo = new MyObject();

if(someObject instance of MyObject)

mo = (MyObject) someObject;

System.out.println("mo was changed to " + someObject);

else

System.out.print("mo was not changed.");

5) Describe the limitations of the copy constructor.


Answer: There are times in which the copy constructor does not work as it was intended, and the
method clone() is a much better choice because of the principal of polymorphism.

6) What is an abstract method?


Answer: Abstract methods do not provide implementations. Abstract methods enable generic
processing of related class objects.
Chapter 8 Polymorphism and Abstract Classes 6

7) What is wrong with the following method definition?

public abstract void doSomething(int count)

Answer: The semi-colon is missing from the end of the method definition. The correct definition
follows:

public abstract void doSomething(int count);

8) Why should the instanceOf operator be used in conjunction with downcasting?


Answer: The instanceOf operator can be used to test if it is sensible to downcast an object. This
makes type casts safer.

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

Dress Shoe Tennis Shoe Boots

10) Implement the base class in the shoe hierarchy in number 8 above.
Answer:

public class Shoe

private String color;


Chapter 8 Polymorphism and Abstract Classes 7

private String designer;

private double size;

public Shoe()

color = "";

designer = "";

size = 0;

public Shoe(String c, String d, double s)

setColor(c);

setDesigner(d);

setSize(s);

public void setColor(String c)

color = c;

public void setDesigner(String d)


Chapter 8 Polymorphism and Abstract Classes 8

designer = d;

public void setSize(double s)

if(s > 0)

size = s;

public String getColor()

return color;

public String getDesigner()

return designer;

public double getSize()

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:

public class DressShoe extends Shoe

private String type; //valid types include pumps, heels and flats

public DressShoe()

super();

type = "";

public DressShoe(String c, String d, double s, String t)

super(c, d, s);

setType(t);

public void setType(String t)

type = t;
Chapter 8 Polymorphism and Abstract Classes 10

public String getType()

return type;

public String toString()

return "Dress shoe designed by " + this.getDesigner() +

"\nColor: " + this.getColor() + "\nSize: " +

getSize() + "\nType: " + getType();

public boolean equals(Object o)

if(o == null)

return false;

else if(getClass() != o.getClass())

return false;

else

{
Chapter 8 Polymorphism and Abstract Classes 11

DressShoe otherDressShoe = (DressShoe) o;

return (getDesigner().equals(otherDressShoe.getDesigner()) &&

getColor().equals(otherDressShoe.getColor()) &&

getSize() == otherDressShoe.getSize() &&

type.equals(otherDressShoe.type));

12) Derive a class named Tennis Shoes from the base class created in number 9 above.
Answer:

public class TennisShoe extends Shoe

private String soleType;

private String canvasType;

public TennisShoe()

super();

soleType = "";

canvasType = "";
Chapter 8 Polymorphism and Abstract Classes 12

public TennisShoe(String c, String d, double s, String st, String ct)

super(c, d, s);

setSoleType(st);

setCanvasType(ct);

public void setSoleType(String st)

soleType = st;

public void setCanvasType(String ct)

canvasType = ct;

public String getSoleType()

return soleType;

}
Chapter 8 Polymorphism and Abstract Classes 13

public String getCanvasType()

return canvasType;

public String toString()

return ("This tennis shoe is designed by: " + this.getDesigner() +

"\nColor: " + getColor() + "\nSize: " + getSize() +

"\nSole type: " + getSoleType() +

"\nCanvas type: " + getCanvasType());

public boolean equals(Object o)

if(o == null)

return false;

else if(getClass() != o.getClass())

return false;

else
Chapter 8 Polymorphism and Abstract Classes 14

TennisShoe otherTennisShoe = (TennisShoe) o;

return

(getDesigner().equals(otherTennisShoe.getDesigner())

&&

getColor().equals(otherTennisShoe.getColor()) &&

getSize() == otherTennisShoe.getSize() &&

soleType.equals(otherTennisShoe.soleType) &&

canvasType.equals(otherTennisShoe.canvasType));

13) Derive a class named Boots from the base class created in number 9 above.
Answer:

public class Boot extends Shoe

private String heelType; //valid types include pumps, heels and flats

public Boot()

{
Chapter 8 Polymorphism and Abstract Classes 15

super();

heelType = "";

public Boot(String c, String d, double s, String ht)

super(c, d, s);

setHeelType(ht);

public void setHeelType(String ht)

heelType = ht;

public String getHeelType()

return heelType;

}
Chapter 8 Polymorphism and Abstract Classes 16

public String toString()

return "Boot designed by " + this.getDesigner() +

"\nColor: " + this.getColor() + "\nSize: " +

getSize() + "\nType: " + getHeelType();

public boolean equals(Object o)

if(o == null)

return false;

else if(getClass() != o.getClass())

return false;

else

Boot otherBoot = (Boot) o;

return (getDesigner().equals(otherBoot.getDesigner()) &&

getColor().equals(otherBoot.getColor()) &&
Chapter 8 Polymorphism and Abstract Classes 17

getSize() == otherBoot.getSize() &&

heelType.equals(otherBoot.heelType));

14) Override the clone method inherited in the Dress Shoes class created in number 10 above.
Answer:

public Object clone()

return new DressShoe(this.getColor(), this.getDesigner(), this.getSize(),

this.getType());

15) Override the clone method inherited in the Tennis Shoes class created in number 11 above.
Answer:

public Object clone()

return new TennisShoe(this.getColor(), this.getDesigner(), this.getSize(),

this.getSoleType(), this.getCanvasType());

16) Override the clone method inherited in the Boots class created in number 12 above.
Answer:

public Object clone()


Chapter 8 Polymorphism and Abstract Classes 18

return new Boot(this.getColor(), this.getDesigner(), this.getSize(),

this.getHeelType());

You might also like