Java Reference
In-Depth Information
As with the is-a relationship, the has-a relationship is an object-oriented concept not
exclusive to Java. Sometimes the relationships between objects is not as obvious as the
Employee , Person , and Address classes, but you should strive to adhere to the is-a and
has-a relationships as much as possible. The benefi ts of adhering to these relationships
include the following:
The resulting code is more logical.
The code is easier to understand.
The classes are easier to reuse in other relationships and applications.
The code is easier to maintain, especially if the needs and requirements of the program
change.
Combining the is-a and has-a relationships with the other OO design goals of
tight encapsulation, loose coupling, and high cohesion results in fl exible, logical, and
maintainable code.
We now change subjects and discuss the details of modifi ers on the fi elds and methods
of inherited classes.
Modifiers and Inheritance
You will be tested on your knowledge about the various Java modifi ers and their effect
on inheritance. In particular, the exam will test your knowledge of the access modifi ers:
public , private , protected , and the default access, as well as the abstract and final
modifi ers. This section discusses the details of these modifi ers on inheritance, starting with
the access modifi ers.
The Access Modifiers
The Java language has four access modifi ers, and it is important to understand their effect
on fi elds and methods. (Expect several questions on the exam that test your understanding
of the access modifi ers.) The access modifi ers are as follows:
public A public fi eld, method, or constructor in a class is accessible to any other class.
private A private fi eld, method, or constructor is only accessible from within the class
it is declared.
protected A protected fi eld, method, or constructor is accessible from other classes in
the same package or subclasses.
No modifier (the default access) A fi eld, method, or constructor with default access is
accessible only from other classes in the same package.
Search WWH ::




Custom Search