• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Is this a Bug in Java

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I made this class hierarchy in Java. Then I wrote this code in main method


This code is not generating any Compile time error. While interface1 and class3 are not related by any means i.e. they don't have a common sub type. Similarly interface1 and interface2 and interface2 and class2 are not related by any means. Then it must produce a compile time error. But it is not... Why is this so???
Is this some sort of bug in Java???

[ August 03, 2008: Message edited by: AnkitJi Garg ]
[ August 03, 2008: Message edited by: AnkitJi Garg ]
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi AnkitJi Garg welcome to Javaranch ,

Then it must produce a compile time error.


Why?

The only thing that will happen is that the instanceof will fail at runtime if the objects are not IS-A.

Hope this helps
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And not really a advanced question, please CarefullyChooseOneForum.

and also take some time to read the ask good questions link below
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all sorry for posting in the wrong section... I thought this is an advanced question....


Isn't it true that for compilation to succeed on the above instanceof operator type1 and type3 must have a common sub-type. If it is true then there must be a compilation error in the original code that I posted.. At run-time the code cannot return true in any circumstances. Then this must be detected by the compiler on compile time....

[ August 04, 2008: Message edited by: AnkitJi Garg ]
[ August 04, 2008: Message edited by: AnkitJi Garg ]
 
Marshal
Posts: 79979
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have misunderstood the workings of the instanceof operator. It does not require much of its left-hand operand; it can be any reference type, or even null (I don't know whether you can use a primitive to the left of instanceof). All that happens if you put something "wrong" to the left of instanceof is that it returns false.

More about instanceof here, and here. Those links suggest you can't use a primitive as an operand to instanceof.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But then why does this code generate a compile time error

It says that class2 is inconvertible to class1 (as they are unrelated). But in my original code interface1 and interface2 are also unrelated. Then how does the compiler not able to see that..
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I modify my code and use interfaces instead of classes, then there is no compilation error.

Is this because the compiler doesn't check for covertibility on interfaces???
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by AnkitJi Garg:
But then why does this code generate a compile time error

It says that class2 is inconvertible to class1 (as they are unrelated). But in my original code interface1 and interface2 are also unrelated. Then how does the compiler not able to see that..



Because the other example uses variables (in other words, object references) - those can change over time, so the compiler doesn't know what it will point to at runtime.

But here it's clear what class is to the left of the instanceof operator, and that it can never be satisfied.
 
Ranch Hand
Posts: 98
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are wrong.



This gives a compiler error:
"Incompatible conditional operand types String and Integer"
 
David Balažic
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additionally:

The above code will not compile, because it is impossible for a reference variable of type String to refer to an object that IS-A Integer. If it would compile, it would always return false. So it is a waste of CPU and the compiler will refuse to compile it.


But in the case of AnkitJi Garg, the case:

The expression in line 2 is not always necessarily false.
Because there could be a class that extends class3 and implements interface1, like this:


Now the same line evaluates to true :

[ August 04, 2008: Message edited by: David Bala�ic ]
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you David Bala�ic. You really cleared my confusion... Now I got it that why in case of classes a compile time error is generated and not in case of interfaces. This is because you can only extend one class but you can implement multiple interfaces.
So if you compare unrelated classes then it is sure at compile time that there can be no class that is a sub type of both the classes.
But if you compare unrelated interfaces or an interface and a class, then the compiler cannot be sure as there can be a class that is a sub-type of both the interfaces.
[ August 04, 2008: Message edited by: AnkitJi Garg ]
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've checked it with some examples, and if either the variable is declared as an interface or the class to check against is an interface, then you don't get a compiler error.

I think David has given the perfect response - because with interfaces there isn't a simple hierarchical class tree, the compiler cannot determine whether or not the actual class is not in the tree.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is impossible to have some class which it subclass of both class2 and class3 so

is illegal.

But is it possible that some class will both be a subclass of class3 and implement interface1 or that a class will implement both interface1 and interface2. So (1) and (2) are correct.

(3) and (4) are correct for very similar reason.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good question by ankitji. Could have been put into advanced forum. Though question deals on fundamentals of java, but for any practical purpose, i will not look for such puzzle questions in beginners level.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic