Java Reference
In-Depth Information
Any method you mark with the @Override annotation causes the compiler to verify that the signature of
the method is the same as a method with the same name in a superclass. If it is not, you get an error message.
It is therefore a good idea to use the @Override annotation for all your methods that override an inherited
method.
CHOOSING BASE CLASS ACCESS ATTRIBUTES
You now know the options available to you in defining the access attributes for classes you expect to use
to define subclasses. You know what effect the attributes have on class inheritance, but how do you decide
which you should use?
There are no hard and fast rules — what you choose depends on what you want to do with your classes in
the future, but there are some guidelines you should consider. They follow from basic object-oriented prin-
ciples:
• You should declare the methods that make up the external interface to a class as public . As long
as there are no overriding methods defined in a derived class, public base class methods are in-
herited and fully available as part of the external interface to the derived class. You should not
normally make data members public unless they are constants intended for general use.
• If you expect other people will use your classes as base classes, your classes will be more secure
if you keep data members private , and provide public methods for accessing and manipulating
them when necessary. In this way you control how a derived class object can affect the base class
data members.
• Making base class members protected allows them to be accessed from other classes in the same
package, but prevents direct access from a class in another package. Base class members that are
protected are inherited in a subclass and can, therefore, be used in the implementation of a de-
rived class. You can use the protected option when you have a package of classes in which you
want uninhibited access to the data members of any class within the same package — because
they operate in a closely coupled way, for instance — but you want free access to be limited to
subclasses in other packages.
• Omitting the access attribute for a class member makes it directly available to other classes in the
same package while preventing it from being inherited in a subclass that is not in the same pack-
age — it is effectively private when viewed from another package.
POLYMORPHISM
Class inheritance is not just about reusing classes that you have already defined as a basis for defining a
new class. It also adds enormous flexibility to the way in which you can program your applications, with a
mechanism called polymorphism . So what is polymorphism?
The word polymorphism generally means the ability to assume several different forms or shapes. In pro-
gramming terms it means the ability of a single variable of a given type to be used to reference objects of
different types and to automatically call the method that is specific to the type of object the variable refer-
ences. This enables a single method call to behave differently, depending on the type of the object to which
the call applies. This is illustrated in Figure 6-4 .
 
Search WWH ::




Custom Search