• 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

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KS && BB says "You can't use the instanceof operator to test across two different class hierarchies. " P-297
Then why the code below is not giving compile time error on line#11:

 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi swaraj, you can use any interface with an instanceof operator. The reason why this is allowed is that there is a possibility of class B (or any class) implementing interface F (or any other interface)

 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

that there is a possibility of class B (or any class) implementing interface F (or any other interface)


Can I have more on this..?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:

that there is a possibility of class B (or any class) implementing interface F (or any other interface)


Can I have more on this..?



Here is a case, where a B reference is pointing to a B object that implements F ...

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an interface can be implemented by any class
we have class B and suppose it has a subclass C that implements the interface F
now
the object of C is of type of B and F also due to polymorphism
but
for B the object of B class cannot be of type F
hence the instanceof test would reuslt to false but it does not give compiler error
look at the code for further understanding
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

b is referring to object of class B
//but at compile time compiler does not know whether b is referring
//to objet of B or object of C so it may think that the object referred
//by b can be of class C



In the example that you have given Prasad, I totally agree that there might be a possibility where b might refer to object of C, but in the example I have given above there is no such possibility I think. Henry, has also specified with the same sort of example that you have used.

Please clarify with the example I have provided.
And if you have any concrete rule to remember this please share that as well.

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:
...
Please clarify with the example I have provided.
And if you have any concrete rule to remember this please share that as well.



I think the reason would be: Interfaces are not part of the class hierarchy. Interfaces can be implemented across the hierarchy (can be implemented by classes from different hierarchy).
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohamed sanaullah wrote:

swaraj gupta wrote:
...
Please clarify with the example I have provided.
And if you have any concrete rule to remember this please share that as well.



I think the reason would be: Interfaces are not part of the class hierarchy. Interfaces can be implemented across the hierarchy (can be implemented by classes from different hierarchy).


well this is true
and concrete rules is this one only
cheers
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi swaraj,
As you are applying an "if(b instanceof F)" test, you will not get a compiler error because it is just a test and the value is false.
So compilation is just fine but nothing will appear at the output when you run this code because if() test fails as you and compiler know that 'b' is not an 'instanceof' 'F' interface.
OK
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruchir Verma wrote:Hi swaraj,
As you are applying an "if(b instanceof F)" test, you will not get a compiler error because it is just a test and the value is false.
So compilation is just fine but nothing will appear at the output when you run this code because if() test fails as you and compiler know that 'b' is not an 'instanceof' 'F' interface.
OK



If you, instaead of using the interface in the test, used the P class as Right Value as the following code:

then you will get this compile error
C:\myprogs>javac Fh.java
Fh.java:11: inconvertible types
found : B
required: P
if(b instanceof P)
^
1 error
reply
    Bookmark Topic Watch Topic
  • New Topic