Programming and Problem Solving with Java

(やまだぃちぅ) #1
7.9 Testing and Debugging | 365

7.9 Testing and Debugging


Testing and Debugging Hints

1.Study the entire hierarchy for a class before you try to use or extend it. Watch
out for cases of overriding and hiding that change the semantics of a class
member.
2.An abstractclass cannot be instantiated as an object; instead, a derived class
must be written to extend it.
3.Overriding applies to instance methods; hiding applies to class methods. You
cannot hide an instance method with a class method or override a class
method with an instance method. However, Java does allow staticand
instance fields to hide each other.
4.When you write a class that extends a superclass, make sure that the compi-
lation unit (program or package) imports the superclass if it isn’t one of the
standard ones that is always imported.
5.The access modifiers for a method that overrides or hides a method in a
superclass must grant the same level of access or greater. The most restrictive
form of access is private, then package, then protected, then public. You can
override a privatemethod with a publicone, but you can’t use privateto over-
ride a publicmethod.
6.Make sure that the first statement of every constructor is a call to one of the
superclass constructors. If you omit the call, Java automatically inserts a call to
super().
7.Overloading a method requires that the two methods have different
signatures. The signature is the name of the method plus the types of its
parameters in a specific order. The return type and modifiers are not part of
the signature.
8.Use superto access fields or methods of a superclass that have been hidden or
overridden by a derived class.
9.Use thisto access instance fields that have been hidden by a local
declaration.
10.Thoroughly document the interface of a class to facilitate its proper use,
ensure its correct design, and simplify testing.
11.If you use any file I/O, remember that mainmust have the throwsIOException
clause appended to its heading.
12.The constructors for arguments to the ObjectInputStreamand
ObjectOutputStreamare instances of FileInputStreamand FileOutputStream,
respectively.
Free download pdf