aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof" Watch "instanceof" New topic
Author

instanceof

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Mar 22, 2010
Posts: 308

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

Joined: Oct 22, 2010
Posts: 181

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
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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

Joined: Mar 07, 2010
Posts: 446
    
    1

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


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Sep 08, 2007
Posts: 3097
    
  33

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

Joined: Mar 07, 2010
Posts: 446
    
    1

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

Joined: Nov 10, 2010
Posts: 10
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

Joined: Nov 02, 2010
Posts: 87
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


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof