• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt in instanceof

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Okay, now Test 1 results in a compilation error. It says inconvertible types because there is no relationship between C1 and C2. (Maybe we can cast ref to (Object)ref to solve the compiler error...)

But my question is why does Test 2 also not show a comilation error..? Because C1 has not implemented I

Thanks!
 
Krishnakumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question please..

If I make two changes to the above code, Test 1 returns false though there is no relationship between C1 and C2.

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 things you need to understand here:
The instanceof operator effectively determines whether the reference value of the object denoted by the reference on the left-hand side is assignable to a reference of the type that is specified on the right hand side.
In the case of Test1,
c1 is not assignable to C2.

In the second case, remember this:
Consider the following:
SorceType srcref;
DestinationType destref;
If SourceType is an interface type, then, the reference value in srcref may be assigned to the destref reference, provided DestinationType is one of the following:
DestinationType is Object
DestinationType is a superinterface of subinterface SourceType

So, in the case of Test2, since ref is an Object, ref instance of I is valid.
[ March 25, 2005: Message edited by: Kedar Dravid ]
 
Krishnakumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kedar, Would you mind explaining the second case in detail. It is still not clear to me.

Thanks!
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kedar Dravid:

If SourceType is an interface type, then, the reference value in srcref may be assigned to the destref reference, provided DestinationType is one of the following:
DestinationType is Object
DestinationType is a superinterface of subinterface SourceType

So, in the case of Test2, since ref is an Object, ref instance of I is valid.



Hi kedar.

I just wanted to clarify what you are saying about test2. Are you saying that interface I is an Object type and because of that, it will compile and run as false.
 
Jeff Tian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am thinking that:

Test1 is correct to fail compilation because the operator "can only be used to test objects against class types in same class hierarchy" (K&B). ie at the level in which they are declared (as opposed to Object level), C1 and C2 are clearly in different class hiearachies. So compilation rightly fails.

I am not sure about Test2, but my guess (a guess coming from someone who hasn't coded in 12 months :roll: but studying for SCJP now ) is that in the case of interfaces, it is still abtract, ie it does not yet belong to any particular class hierarchy.

Hopefully someone can confirm this.
[ March 25, 2005: Message edited by: Jeff Tian ]
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The trick with instanceof and interfaces is that is would be possible for a subclass of the class you are comparing to to implement the interface.
example:

This is because we could later define a new class

in which case a class of type A could be also of type I.

Because any class can implement any interface at any time and instance of with an interface will always compile.
 
Krishnakumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve and others for your response..
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a record for late replies?

Except for the special gotcha case where class A in the example immediately preceding is declared final, because then the compiler knows that no subclass can come along and implement the interface later, so it jumps on its chance to give you a compile failure and happily does so.

I never knew that until reading the book by the Enthuware team....I am reading the Sybex book now and looking to see if that high-dollar Jeopardy answer is in there too.

It appears that the Certification Exams are always Tricky and often Very Tricky, but maybe not *quite* that Tricky?
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if it's a late-reply record, but it's refreshing that it isn't "Did you get the answer to this question yet?" But anyway, thanks for posting that point, which might well gotcha in a certification exam.
 
Those are the largest trousers in the world! Especially when next to this ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic