Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator" Watch "instanceof operator" New topic
Author

instanceof operator

Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

Hi All,

Please assist to analyze the following code:



Doubts:
1. why is it that line 1 and 2 do not compile when they are uncommented (we get inconvertible type error)
2. All the other classes eventhough they are not implementers of the interface I still the error inconvertible types doesn't come.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

The compiler is only looking at the reference types. It doesn't care that you've just created a new object. So, for instance, new X() is treated as a variable of type X. And the compiler will allow any comparison that could be true. It won't accept anything that cannot possibly be true, as that must be a mistake by the programmer (why check something that can't be true?).

Y implements I, so that check on line 13 will always be true. Line 13 might initially look as if it couldn't be true, but remember that you can do this:
class A extends X implements I {}

Now, can a V reference possibly point to something that implements I?

Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

Hi Matthew,

I probably dint get that part. Please help to elaborate.

I actually never expected such a syntax esp [] being compared with instance of operator. Also I am not sure why with the final class alone it fails.

Thanks
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Matthew Brown wrote:Now, can a V reference possibly point to something that implements I?

This part Kaur? If you see there's an extra modifier for the class V?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Kaur Manpreet wrote:I probably dint get that part. Please help to elaborate.

I actually never expected such a syntax esp [] being compared with instance of operator. Also I am not sure why with the final class alone it fails.

Don't worry about the arrays for now. The principle for the first set of statements is exactly the same as for the second.

Because X doesn't implement I, the only way for an X reference to contain an I is if there is a subclass of X that implements I. This is possible, so line 13 compiles.

Because V doesn't implement I, the only way for an V reference to contain an I is if there is a subclass of V that implements I. But V is final. What does that mean, and do you see why it matters?
Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

Please correct me, what I understand is
1. instanceof operator looks at the reference type (not the object)
2. if a class is not marked final, then it can be subclassed. A subclass can choose to implement the interface. In that case the superclass can pass the instanceof test.

Doubts/Misunderstandings:
1. I actually thought that instanceof is looking at the actual object type being referred by the reference type.
2. The part about a subclass implementing an interface is a possibility, so is it that compiler allows this to pass through that it might happen in future?
3. If I replace the interface I, with a class lets say Thread, for lines 13 to 17, all of them give inconvertible types compile time error. Is the reason behind it that, as Java supports single inheritance, so subclasses cannot be instances of Thread and if we want the existing class like W to be an instance of Thread it must be modified and then its a new class altogether, so as is it cannot be instance of Thread.
4. Again if we replace it with Runnable, the rules for the interface above apply.

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Kaur Manpreet wrote:Doubts/Misunderstandings:
1. I actually thought that instanceof is looking at the actual object type being referred by the reference type.

At runtime, it will do - that's what the operator is for, after all. But the compiler will only look at the reference type, and prevents any cases where the runtime check can't possibly succeed.
2. The part about a subclass implementing an interface is a possibility, so is it that compiler allows this to pass through that it might happen in future?

Yes. The compiler can't possibly tell what subclasses there might be. Unless it's marked final, of course.
3. If I replace the interface I, with a class lets say Thread, for lines 13 to 17, all of them give inconvertible types compile time error. Is the reason behind it that, as Java supports single inheritance, so subclasses cannot be instances of Thread and if we want the existing class like W to be an instance of Thread it must be modified and then its a new class altogether, so as is it cannot be instance of Thread.

Got it!
Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

Thanks All
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Matthew Brown wrote:
Because V doesn't implement I, the only way for an V reference to contain an I is if there is a subclass of V that implements I. But V is final. What does that mean, and do you see why it matters?

I don't know whether I am getting it right, but, do you mean that: -
If Class A has a subclass B, and B implements I, then class A has got an IS-A relationship with I??
This means that, if a subclass, say, Stray Dog of Animal, is a Dog with bark() method, then Animal is also a Dog with a bark() method??

Am I getting you right?? If yes, How come?? Just got confused..
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

R. Jain wrote:
Matthew Brown wrote:
Because V doesn't implement I, the only way for an V reference to contain an I is if there is a subclass of V that implements I. But V is final. What does that mean, and do you see why it matters?


According to me, it should be like: -
If V doesn't implement I, the only way for an V reference to contain an I is if there is a superclass of V that implements I? Am I wrong somewhere??
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

R. Jain wrote:
R. Jain wrote:
Matthew Brown wrote:
Because V doesn't implement I, the only way for an V reference to contain an I is if there is a subclass of V that implements I. But V is final. What does that mean, and do you see why it matters?


According to me, it should be like: -
If V doesn't implement I, the only way for an V reference to contain an I is if there is a superclass of V that implements I? Am I wrong somewhere??


Oh My God.. What I just wrote, I am sorry for that. Don't believe I just wrote that!!!
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

R. Jain wrote:
Matthew Brown wrote:
Because V doesn't implement I, the only way for an V reference to contain an I is if there is a subclass of V that implements I. But V is final. What does that mean, and do you see why it matters?

I don't know whether I am getting it right, but, do you mean that: -
If Class A has a subclass B, and B implements I, then class A has got an IS-A relationship with I??
This means that, if a subclass, say, Stray Dog of Animal, is a Dog with bark() method, then Animal is also a Dog with a bark() method??

Am I getting you right?? If yes, How come?? Just got confused..



no Class A wont be having IS-A relatino with I. instanceof operator only looks at the TYPE of ACTUAL OBJECT referenced and returns true or false accordingly. wha this question demands of you is "TO BE A COMPILER" AND THINK LIKE A COMPILER". let me repeat matthews words in another way. COMPILER won't allow anything which is syntactically incorrect and which according to compiler is DEAD SURE TO FAIL AT RUNTIME. BUT, (and this part is important) it WILL ALLOW ANYTHING THAT CAN PASS AT RUNTIME. now in the original question new X() IS-NOT I, but still compiler allows it ? (keep in mind it will fail at runtime) . why ? as matthew explained there could have been a class say Test which extends X and implements I . for eg class Test extends X implement I. now when the compiler sees new X() instance of I, it just sees the reference which is of the TYPE X. it does not know what is at the other end of the reference(that is only determined at runtime). so the compiler thinks it can be possible that the X reference refers to Test object. i.e X x = new Test(). if this is the case then surely the instanceof test will pass. so the compiler allows SUCH POSSIBLE SITUATIONS. However in the last case, when the class is final, compiler KNOWS FOR CERTAIN that reference of type V cannot ever ever point to something which is I. why ? because V is final . you cannot subclass it. which means no subclass to implement I.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator
 
Similar Threads
//question of round-up//
4m Dan's Exam
once again instanceof
anyone could explain this?
Problem with instanceof