Java Reference
In-Depth Information
What defines a particular view of a Java object and how do you get that view of the object? A view is something
that is available for an outsider (in technical terms, for clients or users of the class). The set of methods (accessible to
clients) defined in a type (a class or an interface) defines a view of an object of that type. For example, the Walkable
type defines one method: walk() . If you get a Walkable view of an object, it means that you have access to only the
walk() method of that object. Similarly, if you have a Swimmable view of an object, you can access only the swim()
method of that object. How about having a Turtle view of a Turtle object? The Turtle class defines three methods:
bite() , walk() , and swim() . It also inherits methods from the Object class. Therefore, if you have a Turtle view of
an object, you can access all methods that are available in the Turtle class (directly declared or inherited from its
superclass and superinterfaces). Every class in Java is inherited, directly or indirectly, from the Object class. By virtue
of this, every object in Java has at least two views: one view defined by the set of methods that are available (declared
or inherited) in the object's class and another view defined by the set of methods defined in the Object class. When
you are using the Object view of an object, you can access methods of only the Object class.
You get different views of an object by accessing it using reference variables of different types. For example, to get
the Walkable view of a Turtle object you do any of the following things:
Turtle t = new Turtle("Turti");
Walkable w2 = t; // w2 gives Walkable view of Turtle object
Walkable w3 = new Turtle(); // w3 gives Walkable view of Turtle object
With this knowledge of a Java object that can support different views, Let's look at the use of the instanceof
operator. It is used to test if an object supports a specific view or not. Consider the following snippet of code:
Object anObject = get any object reference;
if(anObject instanceof Walkable) {
// anObject has a Walkable view
Walkable w = (Walkable)anObject;
// Now access the Walkable view of the object using w
}
else {
// anObject does not have a Walkable view
}
The anObject variable refers to an object. The instanceof operator is used to test if the object to which anObject
variable refers to supports a Walkable view. Note that just defining a walk() method in a class does not define a
Walkable view for the objects of that class. The class must implement the Walkable interface and implement the
walk() method in order for its object to have a Walkable view. A view of an object is synonymous with its type. Recall
that implementing an interface to a class gives the objects of that class an additional type (that is an additional view).
How many views an object of a class has? An object of a class can have the following views:
A view that is defined by its class type
Views that are defined by all superclasses (direct or indirect) of its class
Views that are defined by all interfaces implements by its class or superclasses
(direct or indirect)
 
Search WWH ::




Custom Search