• 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

instanceof example compilation error

 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a compilation error on line 7.  Cat is related to Animal class and ChildBear is also related to Animal class. So both are related to each other. So why am I getting a compilation error that both are incompatible? I would appreciate a detailed explanation.

Scenario:
Bear extends Animal
Cat extends Animal
ChildBear extends Bear



 
Ranch Hand
Posts: 109
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the compilation error? Which is line 7? Line 7 in your original code may not be the same as line 7 in your posted code.
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line which gives compilation error
 
Sergiu Dobozi
Ranch Hand
Posts: 109
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raja singh kumar wrote:The line which gives compilation error



Could you try writing System.out.println((Object) cat instanceof ChildBear); ?
Maybe that will work.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raja singh kumar wrote:Cat is related to Animal class and ChildBear is also related to Animal class. So both are related to each other

No, Cat and ChildBear are not related to each other. You can't relate Cat and ChildBear just because both are child classes of Animal then in that case every class we create in Java should be related to each other as all of them directly Or indirectly subclass of Object class. Isn't It?
You can see in your mentioned scenario there is no Cat extends ChildBear.

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

JLS 15.20.2 wrote:At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.



ChildBear -->Bear --> Animal -->Object

In main method of your example you can try such combinations to understand It more


In above code:
Line no 7-8:  
  • It compiles successfully because there is relation between class Animal and Object i.e. Animal is subclass of Object.
  • Both expression on line 7 and 8 returns true because at run time an object of Bear is created which can be casted to Object and Animal without raising a ClassCastException.

  • Same with next lines of code except

    Line no 13: bear instanceof ChildBear
  • It compiles successfully because there is relation between Bear and ChildBear, the reference variable bear can hold an object of Bear Or ChildBear which is determined at run time where an object of Bear is created which can't be cast to reference type i.e. ChildBear because ChildBear is subclass of Bear so this expression returns false.


  • Line no 22: cat instanceof Bear
  • Gives compile time error because compiler is smart enough to determine reference type at compile time. It knows reference type Cat can never hold an object of Bear as there is no relation between Cat and Bear which results in compile time error.


  • Line no 23: animal instanceof ChildBear
  • Compiles successfully because there is relation between Animal and ChildBear ( ChildBear -->Bear --> Animal ), here reference type Animal can hold an object of Animal Or Bear Or ChildBear Or Cat but at run time object of Bear is created which can't be cast to ChildBear so returns false.
  •  
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why are you trying to use instanceof in the first place? Frequent use of instanceof outwith an equals() method leads us to suspect there is something wrong with your inheritance hierarchy.
     
    raja singh kumar
    Ranch Hand
    Posts: 212
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for your detailed explanation. I do have a few questions on your reply. I will ask one by one

    With reference to the diagram below,

    1. Will a compile time error occur when I pick a reference of a class from one path and use instanceof operator with a class from a different path?




    hier.png
    [Thumbnail for hier.png]
    hierarchy
     
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, because the compiler knows that the object referenced by the reference type cannot possibly match the class you are checking against.
     
    raja singh kumar
    Ranch Hand
    Posts: 212
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, thanks.

    Now if suppose I use a reference and class from the same path while using instanceof. With reference to the diagram again, if I use the reference of a class lower in the hierarchy and use instanceof with a class which is higher in the hierarchy it is going to return true because it satisifies IS-A relationship. Is the understanding correct?
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You might need to show us a code example.  I'm not sure what dircetion your "higher" and "lower" represent.

    This:

    will return false, for example.
     
    raja singh kumar
    Ranch Hand
    Posts: 212
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, so can I say an object of the child class is an instance of the parent class while the instance of the parent class is not an instance of the child class?
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You could do a simple test yourself, using your existing code.
    But yes.

    In your case, an instance of Animal is not an instance of Bear, but an instance of Bear is an instance of Animal.
     
    raja singh kumar
    Ranch Hand
    Posts: 212
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic