aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof comparing two different branches of the inheritance tree 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 comparing two different branches of the inheritance tree" Watch "instanceof comparing two different branches of the inheritance tree" New topic
Author

instanceof comparing two different branches of the inheritance tree

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Enthuware question:

Expression (s instanceof java.util.Date) will return false if 's' was declared as a variable of class java.lang.String.


- True
- False


Answer is false. Below is explanation:

It will not even compile because the compiler knows that 's' (which is declared as of class String) can NEVER refer to an object of class java.util.Date. So, it will not accept this code.
Had 's' been declared as a variable of type Object, this code would have compiled because compiler sees that at run time it is possible for s to refer to an object of class Date.


So if I understand, java.util.Date and java.util.String are two separate branches originating from Object. Because java.util.Data and java.util.String are two separate branches, the compiler will not accept an instanceof comparison. Is my understanding correct?


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Correct! With the class type references, you can't use the instanceof operator with different hierarchy. But, it'k OK, if they are with the interface reference types!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Correct! With the class type references, you can't use the instanceof operator with different hierarchy. But, it'k OK, if they are with the interface reference types!


i didn't get the last line Abimaran wrote


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

you can use any non-final class as the left side operand for the instanceof operator,if the right side operand is an interface(can use any interface).


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

you can use any non-final class as the left side operand for the instanceof operator,if the right side operand is an interface(can use any interface).



i think final as well as abstract classes can used as the left side operand for the instanceof operator,if the right side operand is an interface(can use any interface).
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:
Correct! With the class type references, you can't use the instanceof operator with different hierarchy. But, it'k OK, if they are with the interface reference types!


i didn't get the last line Abimaran wrote

That's fine. Let's have an example then.

Could you able to compile this? If not what is the reason? Any Guess. This is what I've mentioned there!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

mohitkumar gupta wrote:
i think final as well as abstract classes can used as the left side operand for the instanceof operator,if the right side operand is an interface(can use any interface).

with final class,it must implement that interface on the right side.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:
i think final as well as abstract classes can used as the left side operand for the instanceof operator,if the right side operand is an interface(can use any interface).

Have a look on above code. It has the answer for all!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Abimaran Kugathasan wrote:
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Good Morning!

From what I understand about Interfaces: they can be implemented by any part of the Java class hierarchy, hence an interface can be on the right-hand side of the instanceof operator.

Here is my confusion. In the code provided




Why is line 14 giving a compiler error? B is a final class, but what is the significance of that.

Perhaps the bigger issue is, why can't we put a final class on the left-hand side of the instanceof operator?
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Sandra Bachan wrote:
Why is line 14 giving a compiler error? B is a final class
why can't we put a final class on the left-hand side of the instanceof operator?

we can put final class on left side also,but in one condition it has to implement the interface given on right side.try this line 14 will compile.

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sandra Bachan wrote:
Why is line 14 giving a compiler error? B is a final class, but what is the significance of that.

Because, Class B is declared as final, and it doesn't implement the Comparable interface. So, Any object with the reference type B, can't be cast to Comparable.
Sandra Bachan wrote:
Perhaps the bigger issue is, why can't we put a final class on the left-hand side of the instanceof operator?

You can put it.

[Edited to correct small typo!] Thanks Arjun Srivastava to indicate it!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Abimaran Kugathasan wrote:
So, Any object with the reference type can't be cast to Comparable.

what is the meaning of this line?
Every object has got a reference type.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:
Abimaran Kugathasan wrote:
So, Any object with the reference type can't be cast to Comparable.

what is the meaning of this line?
Every object has got a reference type.

It's a typo. I've corrected there! Thanks!~
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I'm puzzled about something. I created code such that B is non-final





It compiles and runs.

However, when I make B final as shown in the following code



it gives following compiler error

InstanceOf.java:12: inconvertible types
found : B
required: java.lang.Comparable
if(new B() instanceof Comparable) {
^
1 error




Please explain why this happens.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Sandra Bachan wrote:
it gives following compiler error
[tt]InstanceOf.java:12: inconvertible types
found : B
required: java.lang.Comparable
if(new B() instanceof Comparable) {
^
Please explain why this happens.


above is your code with some modifications,can you see the difference,also see the posts discussed above.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Abimaran Kugathasan wrote:
Because, Class B is declared as final, and it doesn't implement the Comparable interface. So, Any object with the reference type B, can't be cast to Comparable.

I think, I've already answered!
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
This code snippit:





This is what I can memorize on the surface: we cannot have a final class on the left side of instanceof Comparable unless it EXPLICITLY implements Comparable. Whereas if class is not final, then compiler will allow non-final class instanceof interface

Still no "ah ha" moment.

Can you point me to a tutorial on this particular subject of final classes tied with inheritance.


Thank you for your patience


Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sandra Bachan wrote:

This is what I can memorize on the surface: we cannot have a final class on the left side of instanceof Comparable unless it EXPLICITLY implements Comparable. Whereas if class is not final, then compiler will allow non-final class instanceof interface

Still no "ah ha" moment.

Can you point me to a tutorial on this particular subject of final classes tied with inheritance.

Here, if a class isn't final, then it can be sub classed, so any of the sub class of that class may implements the Comparable(for an example only). When that happens, an object of the sub class can be polymorphically referred with the non-Comparable implementing super class. So, the compiler allows the casting, because the actual object type implements the Comparable interface.

But, if the class is final and not implementing the Comparable interface, then there is no way to insert the Comparable interface reference in the hierarchy below this class. So, Compiler flags a error!

Does this makes sense?
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Abimaran Kugathasan wrote:
Here, if a class isn't final, then it can be sub classed, so any of the sub class of that class may implements the Comparable(for an example only). When that happens, an object of the sub class can be polymorphically referred with the non-Comparable implementing super class. So, the compiler allows the casting, because the actual object type implements the Comparable interface.

But, if the class is final and not implementing the Comparable interface, then there is no way to insert the Comparable interface reference in the hierarchy below this class. So, Compiler flags a error!

Does this makes sense?



What you are explaining makes absolute sense. In my mind was thinking, "Why would the designers of the Java programming class make it illegal for final class to be on the left-hand side of the instanceof operator." Perhaps when thinking of hierarchy, one thinks upwards/superclasses, i.e. grandparents, great-grandparents, rather than downwards/subclasses, i.e. grandchildren, great-grandchildren.

We tend to look at ballerinas too closely to understand and appreciate the illusion and beauty of a ballet performance.

There are several instanceof questions in Generics and Collections of Enthuware, that I didn't grasp 100%, which touch on the concept of class hierarchy. When I re-do exam questions from Generics and Collections, I will have an understanding and appreciation of this concept.

Will mark the thread as solved.

Thank you all!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sandra Bachan wrote:
Will mark the thread as solved.

So, you've cleared the doubt?
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

thanks god!!finally
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instanceof comparing two different branches of the inheritance tree