Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Interface instanceof

Thread view: 
gk - 14 Sep 2006 17:35 GMT
interface MyInterface {

}

public class MyInstanceTest implements MyInterface {

    static String s;

    public static void main(String args[]) {

        MyInstanceTest t = new MyInstanceTest();

        if (t instanceof MyInterface) {

            System.out.println("I am true interface");

        } else {

            System.out.println("I am false interface");

        }

        if (s instanceof String) {

            System.out.println("I am true String");

        } else {

            System.out.println("I am false String");

        }

    }

}

output:

I am true interface
I am false String

this is quite confusing.

see  the code...
 if (t instanceof MyInterface)
 t is not polymorphically created even .....its a damn instance of
MyInstanceTest  class .
i dont understand how its going to become an instance of MyInterface
and the output "I am    true interface"

this would have been if we had the code like this..

        MyInterface t = new MyInstanceTest();  // but its not here

And about the String class  see the code...
if (s instanceof String)

is this if block is false because of s is not created via new operator
?

Please explain
Oliver Wong - 14 Sep 2006 18:47 GMT
> interface MyInterface {
>
[quoted text clipped - 38 lines]
>
> this is quite confusing.

[...]

> is this if block is false because of s is not created via new operator
> ?

   It's false because s is null, and null is considered to not be an
instanceof anything (not even Object).

   - Oliver
gk - 14 Sep 2006 18:56 GMT
what about  t ?

> > interface MyInterface {
> >
[quoted text clipped - 48 lines]
>
>     - Oliver
Matt Humphrey - 14 Sep 2006 20:32 GMT
> what about  t ?

<snip code>

t points to an object of class MyInstanceTest which implements MyInterface
which means t's object is also an instance of MyInterface. This is how Java
gets its multiple inheritance.  Like Oliver pointed out with "s" if t were
null it wouldn't be an instance of either MyInstanceTest or MyInterface.

Instance of is determined by looking at the class of the object at
runtime--not by looking at the type represented by the variable.

Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
gk - 14 Sep 2006 18:57 GMT
what about  t ?

> > interface MyInterface {
> >
[quoted text clipped - 48 lines]
>
>     - Oliver
Patricia Shanahan - 14 Sep 2006 22:23 GMT
> interface MyInterface {
> }
[quoted text clipped - 14 lines]
>     }
> }

To understand the output, see the Java Language Specification, 15.20.2
Type Comparison Operator instanceof,
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#80289

"At run time, the result of the instanceof operator is true if the value
of the RelationalExpression is not null and the reference could be cast
(§15.16) to the ReferenceType without raising a ClassCastException.
Otherwise the result is false."

(x instanceof Y) effectively asks "Does x currently point to an object
that could also be pointed to by a reference expression of type Y?"

(t intanceof MyInterface) is true because t points to an object, an
instance of MyInstanceTest, that could be pointed to by a reference
expression of type MyInterface.

(s instanceof String) is false because s does not point to any object at
all.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.