• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question related to instanceof operator from Khalid Book

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Ans] a




Why x instanceof J is true? Because x is of type I. And there is no IS-A relation between I and J.

Above code prints the output : I J C D in different lines.

It would be great if someone explains why if(x instanceof J) is true?

Thanks in advance.

Regards,
Gopal
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Class D implements Interface J, and "x is an Object of the Class D"

Hence, x is an instance of D and as D implements J,
x is also an instance of J.
Hence, is true.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it checks the actual object at runtime which is of type D. D will pass the instanceOf true for all the 4 types here.

try :


see what happens
 
V. Potluri
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudarshan and Vijitha,

First of all I will be thankful for spending your valuable time for claring my doubt.

Still I am not clear about above code?


According to my knowledge, above code will be checked at compile time and time time.

During compile time:
Is-A relation exists between type of x(here i.e. I) and J?

But IS-A relation is not satisfied in case of

According to my Knowledge, compile time error should be genereated because there is no IS-A relationship between type of x and J

If the compile time check passes, then at runtime actual object class referenced by x is same or subtype of J.

If above two cases satisfies, then
output will be printed.

Please correct me if my understanding is wrong? Waiting for reply!

Thanks in advance.

Regards,
Goapl
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

the instaceof() wont check with the refernce. it will check whether the object has an IS A relation ship with the specified class or interface. in that case, it will return true.
right?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. the compiler error is produced only in case of two different class hierarchies.

2. class D directly implements J & also extends C which implements I. So any object of D IS-A I.

Also please go through this thread.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have some fun and replace the line with . Then you'll see what is explained. Another way of looking at the problem is if the answer is false, what is the exact meaning of 'instanceof'? If it was false, 'instanceof' is useless.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic