Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic