Java Reference
In-Depth Information
We discussed the effect of access modifi ers and inheritance. There are four access
modifi ers in Java that can be applied to the fi eld, methods, and constructors of a class. The
public modifi er provides access to everyone; the private modifi er provides access from only
within the class; the protected modifi er provides package-level access as well as child classes;
and the default access is package-level only. We also discussed the effects of the abstract and
final modifi ers on methods and inheritance. An abstract method must be overridden by
any non- abstract child class, and a final method cannot be overridden in any child class.
The topic of polymorphism was discussed in detail. Polymorphism is when an object
takes on many forms. The typical use of polymorphism in Java is when a parent class
reference points to a child class object. In this situation, the child object is said to “take
on the form” of the parent class. We discussed how to use the instanceof operator to
ensure valid casting, and we also discussed polymorphism in action with the examples of
polymorphic parameters and heterogeneous collections.
Be sure to test your knowledge of these OO concepts by answering the Review
Questions that follow the section on Exam Essentials. Attempt to answer the questions
without looking back at the pages of this chapter. Make sure you have a good
understanding of the following Exam Essentials before you attempt to answer the Review
Questions, and good luck!
Exam Essentials
Understand encapsulation, coupling, and cohesion. Be sure to know what it means for
the fi elds of a class to be tightly encapsulated, where the fi elds of a class are private and
accessed via public methods. Also, you need to know the benefi ts of loose coupling
and high cohesion, which results in code that is more reusable and easier to maintain.
Understand polymorphism and the “is-a” relationship. An object takes on many forms.
For example, a parent class reference can refer to a child class object because the child object
“is-a” parent. Use the is-a relationship as a simple test to ensure you are using inheritance
properly. Use the has-a relationship to determine if you are using composition properly.
Recognize valid reference casting. Given multiple class defi nitions, you need to be able to
determine if a reference cast is successful at compile or runtime. The compiler cannot
always determine if a cast is appropriate, so be able to recognize when a ClassCastException
is thrown.
Know the four levels of access in Java. The members of a class can be public , private ,
protected , or have the default access. You need to recognize whether an attempt to access
a fi eld, method, or constructor of a class is allowed.
Search WWH ::




Custom Search