Java Reference
In-Depth Information
3 Polymorphic Behavior Provided by Inheritance (continued)
2. Create an abstract class, Bounceable, which inherits from Ball. Bounceable
has a single abstract method, bounce(), which returns a String. There are no
instance variables.
3. Create two concrete classes, Baseball and Bowlingball, which inherit from Ball.
No instance variables or additional methods are required, only implementation
of any inherited abstract methods.
4. Create two concrete classes, Tennisball and Basketball, which inherit from
Bounceable. No instance variables or additional methods are required, only
implementation of any inherited abstract methods.
5. Compile all source code and execute the test program to obtain the output
as displayed in Figure 10-61c. The program, PolyTest.java, creates an array of
Ball and references the various objects polymorphically through the parent
class, using downcasting to access the bounce() method when appropriate.
Ball
type
getType()
play()
Bowlingball
Baseball
Bounceable
bounce()
Tennisball
Basketball
FIGURE 10-61a
 
Search WWH ::




Custom Search