Java Reference
In-Depth Information
2 Using Abstract Classes and Methods
Create the Java source code files for the classes listed in the inheritance hierarchy
displayed in Figure 10-60a. Create a new Java source code file and enter code for
the AbstractTest.java test program, as shown in Figure 10-60b. Compile all
source code and execute the test program to obtain the output as displayed in
Figure 10-60c. Perform the following steps to complete these tasks:
1. Create an abstract base class, Animal. The single constructor requires a
String to indicate the type of animal, which then is stored in an instance
variable.
2. Create a concrete class, Cat, which inherits from Animal. Cat has two
instance variables, both Strings: name, which has private access and breed,
which has protected access.
3. Create an abstract class, Bird, which inherits from
Animal. Bird has a single instance variable, breed, of
type String with protected access. Bird implements only
the required move() method.
4. Create a concrete class, Robin, which inherits from Bird.
Robin has a single instance variable, name, of type String
with private access. Robin implements the remaining
required methods.
5. Compile all source code and execute the test program to
obtain the output as displayed in Figure 10-60c.
6. Create a Dog class and a
Fish class, both inheriting
from Animal.
7. Re-write AbstractTest
(Figure 10-60b) as LoopTest.java.
Create an arrray of Animal and then
loop through the array using an
enhanced for(), calling the describe(),
sound(), sleep(), and move() methods,
successively. Save and then print your
source code.
8 Change LoopTest.java to use an
ArrayList instead of an array. Print your
source code and indicate whether or not
your for() loop had to be modified when
changing from an array to an ArrayList.
Animal
type
describe()
sound()
sleep()
move()
J ava 2
J ava 2
v 5 . 0
v 5 . 0
Cat
Bird
name
breed
breed
move()
Robin
name
FIGURE 10-60a
 
Search WWH ::




Custom Search