aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof doubt 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 "instanceof doubt" Watch "instanceof doubt" New topic
Author

instanceof doubt

vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
interface intf { }

class test {}

class test1
{
public static void main(String[] args)
{
test1 tt = new test1();
System.out.println(tt instanceof intf);// No compiler time error
System.out.println(tt instanceof test); // Compiler time error
}
}

When class is not inherting another class then instanceof throws compile time error

When class is not inherting another interface then instanceof not throwing compile time error

can anybody explain why..??

Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
Hi Vidya Sagar

At compile time, the compiler checks that if each of the left hand and right hand operands of the instanceof operator can denote a common subtype of both the operands, if not then they have no obvious relations and compiler throws an error.

taking the above examples:

tt instanceof intf

If we create a class which is subtype of both of above, any of the above references will be able to denote that subtype, but:

tt instanceof test

if we create a class which is common subtype of both of above (which we really can't because multiple inheritence is not allowed in Java), both of references above will not be able to denote that subtype...

Hope this helps...

- Saurabh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof doubt