|
|
|
|
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.
- Create two classes, A and B, with default constructors (empty
argument lists) that announce themselves. Inherit a new class called C
from A, and create a member of class B inside C. Do not
create a constructor for C. Create an object of class C and
observe the results.
- Modify Exercise 1 so that A and B have constructors with
arguments instead of default constructors. Write a constructor for C and
perform all initialization within C’s constructor.
Create a simple class. Inside a second class, define a reference to an
object of the first class. Use lazy initialization to instantiate this object.
- Inherit a new class from class Detergent. Override
scrub( ) and add a new method called sterilize( ).
Take the file Cartoon.java and comment out the constructor for the
Cartoon class. Explain what happens.
- Take the file Chess.java and comment out the constructor for the
Chess class. Explain what happens.
- Prove that default constructors are created for you by the compiler.
Prove that the base-class constructors are (a) always called and (b) called
before derived-class constructors.
- Create a base class with only a nondefault constructor, and a derived class
with both a default (no-arg) and nondefault constructor. In the derived-class
constructors, call the base-class constructor.
- Create a class called Root that contains an instance of each of the
classes (that you also create) named Component1, Component2,
and Component3. Derive a class Stem from Root that also
contains an instance of each “component.” All classes should have
default constructors that print a message about that class.
- Modify Exercise 10 so that each class only has nondefault constructors.
- Add a proper hierarchy of dispose( ) methods to all the classes
in Exercise 11.
- Create a class with a method that is overloaded three times. Inherit a new
class, add a new overloading of the method, and show that all four methods are
available in the derived class.
- In Car.java add a service( ) method to Engine and
call this method in main( ).
- Create a class inside a package. Your class should contain a
protected method. Outside of the package, try to call the
protected method and explain the results. Now inherit from your class and
call the protected method from inside a method of your derived class.
- Create a class called Amphibian. From this, inherit a class called
Frog. Put appropriate methods in the base class. In main( ),
create a Frog and upcast it to Amphibian and demonstrate that all
the methods still work.
- Modify Exercise 16 so that Frog overrides the method definitions
from the base class (provides new definitions using the same method signatures).
Note what happens in main( ).
- Create a class with a static final field and a final field
and demonstrate the difference between the two.
- Create a class with a blank final reference to an object. Perform
the initialization of the blank final inside all constructors.
Demonstrate the guarantee that the final must be initialized before use,
and that it cannot be changed once initialized.
- Create a class with a final method. Inherit from that class and
attempt to override that method.
- Create a final class and attempt to inherit from it.
Prove that class loading takes place only once. Prove that loading may be
caused by either the creation of the first instance of that class or by the
access of a static member.
- In Beetle.java, inherit a specific type of beetle from class
Beetle, following the same format as the existing classes. Trace and
explain the output.
|
|
|