Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface" Watch "Interface" New topic
Author

Interface

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Does Interface pass the IS-A java.lang.Object test?

Isnt it true that Interface does not belong to the Object Inheritance tree?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
The way the instanceof operator works is:
<reference> instanceof <class or interface>

Notice that <reference> must always be a reference to an object, or null in order for the operator to work.

When you ask if Instance passes the Is-A test for Object, I assume you mean to ask if:
i instanceof Object would return true, where i is an instance of Instance.

There are a couple of problems with that scenario. First, I am not aware of a class named Instance (at least I can't find it in the API,) which leads me to believe that instances are implemented as pure abstract Class objects (but this is just speculation.)
The other problem is that you can't instantiate an interface directly (only via a concrete implementing class of the instance,) so the hypothetical reference i would never exist.

However, classes can implement interfaces, and any instance of a class that implements an interface passes the Is-A test for that interface (although it doesn't mean the instance was instantiated directly, as I mentioned before.)
In this scenario, if you have:
instance I {};
class A extends I {};
A a = new A();
Then a instanceof Object will be true, since A (like any other class) implicitly extends Object.
[ December 17, 2008: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
No, interfaces do not inherit from Object class. They are given special compiler treatment in which the *public* methods of the Object class form a part of the interface at compile time.

Therefore, in the following code you can invoke all the methods of the Object class on the interface reference except clone() and finalize() as they are both protected.



This link explains in detail link
[ December 17, 2008: Message edited by: Harvinder Thakur ]

thanks
Harvinder
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Harvinder is correct in saying that interfaces do not inherit from java.lang.Object, although interfaces do include abstract declarations of the public methods defined in java.lang.Object (see the third bullet point under JLS - 9.2).

Ruben is also correct in pointing out that, "you can't instantiate an interface directly (only via a concrete implementing class of the instance)..." So as he explained, if x is a reference with an interface as its type, then x instanceof Object will evaluate to true. However, this is because the object implementing the interface x inherits from Object -- not because the interface IS-AN Object.

But to demonstrate this, you want to use the interface (not the class) as the type...

[ December 17, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Thanks, Marc. That's very interesting (interfaces declaring abstract methods of object.) That makes sense though, because that allows these methods to be accessible from an interface reference, overriding to come into play when a method such as toString() is called using an interface reference. I have never thought of overriding to be used with instance references, but it does take place:


[ December 17, 2008: Message edited by: Ruben Soto ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ruben Soto:
... That's very interesting (interfaces declaring abstract methods of object.) That makes sense though, because that allows these methods to be accessible from an interface reference...

Exactly. An interface reference wouldn't be as useful if it didn't automatically include basic methods like equals, hashCode, toString, etc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interface