Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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:

 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • 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: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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..?
 
Henry Wong
author
Marshal
Pie
Posts: 21001
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 ...

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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.

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ruchir Verma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic