Java Reference
In-Depth Information
12. Within main(), also instantiate an Artist object. Using your Artist
object, invoke drawShape() three times, once for each of the three
shapes instantiated in the previous step.
13. Save, compile, and run the Artist class.
Lab 8.2: Abstract Classes
The purpose of this lab is to become familiar with writing abstract classes
and methods. This lab is a continuation of the work you did in Lab 8.1.
1. Within your Shape class from the previous lab, remove the body of
the draw() method, and declare the method abstract.
2. Save and compile your Shape class.
3. Write a class named Ladder that extends Shape. Add a field of type
int to represent the number of rungs in the ladder, and add a con-
structor that initializes this field. Use encapsulation to ensure the
number of rungs is between 1 and 10.
4. Your Ladder class needs to override draw(). Using asterisks, draw a
ladder with the appropriate number of rungs. For example, a five-
rung ladder should look similar to:
*************************
* * * * *
* * * * *
*************************
5. Save and compile the Ladder class.
6. Modify your ArtistDemo program so that it also instantiates and
draws a Ladder object.
Summary
Polymorphism is the ability of an object to take on many forms. The
most common use of polymorphism in OOP occurs when a parent
class reference is used to refer to a child class object.
■■
There are two key benefits to polymorphism: polymorphic parameters
and heterogeneous collections.
■■
Search WWH ::




Custom Search