|
|
|
|
Exercises
Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.
- Add a new method in the base class of Shapes.java that prints a
message, but don’t override it in the derived classes. Explain what
happens. Now override it in one of the derived classes but not the others, and
see what happens. Finally, override it in all the derived classes.
Add a new type of Shape to Shapes.java and verify in
main( ) that polymorphism works for your new type as it does in the
old types.
- Change Music3.java so that what( ) becomes the root
Object method toString( ). Try printing the Instrument
objects using System.out.println( ) (without any casting).
Add a new type of Instrument to Music3.java and verify that
polymorphism works for your new type.
- Modify Music3.java so that it randomly creates Instrument
objects the way Shapes.java does.
- Create an inheritance hierarchy of Rodent: Mouse,
Gerbil, Hamster, etc. In the base class, provide methods that are
common to all Rodents, and override these in the derived classes to
perform different behaviors depending on the specific type of Rodent.
Create an array of Rodent, fill it with different specific types of
Rodents, and call your base-class methods to see what happens.
Modify Exercise 6 so that Rodent is an abstract class. Make
the methods of Rodent abstract whenever possible.
- Create a class as abstract without including any abstract
methods and verify that you cannot create any instances of that class.
Add class Pickle to Sandwich.java.
- Modify Exercise 6 so that it demonstrates the order of initialization of
the base classes and derived classes. Now add member objects to both the base
and derived classes and show the order in which their initialization occurs
during construction.
- Create a base class with two methods. In the first method, call the second
method. Inherit a class and override the second method. Create an object of the
derived class, upcast it to the base type, and call the first method. Explain
what happens.
- Create a base class with an abstract print( ) method
that is overridden in a derived class. The overridden version of the method
prints the value of an int variable defined in the derived class. At the
point of definition of this variable, give it a nonzero value. In the base-class
constructor, call this method. In main( ), create an object of the
derived type, and then call its print( ) method. Explain the
results.
- Following the example in Transmogrify.java, create a Starship
class containing an AlertStatus reference that can indicate three
different states. Include methods to change the states.
- Create an abstract class with no methods. Derive a class and add a
method. Create a static method that takes a reference to the base class,
downcasts it to the derived class, and calls the method. In main( ),
demonstrate that it works. Now put the abstract declaration for the
method in the base class, thus eliminating the need for the downcast.
|
|
|