Forums Register Login

Interface

+Pie Number of slices to send: Send
Does Interface pass the IS-A java.lang.Object test?

Isnt it true that Interface does not belong to the Object Inheritance tree?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
 

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.
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 856 times.
Similar Threads
inner interface.........help me
Interfaces in java
why did't cast the object to TestHome type?
What does this mean?
java.rmi.MarshalException
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:16:43.