my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes using instanceof in Collection framework 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 "using instanceof in Collection framework" Watch "using instanceof in Collection framework" New topic
Author

using instanceof in Collection framework

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
import java.util.*;
class B {
public static void main (String args[]) {
AbstractMap a = new HashMap(); //line1
AbstractList b = new ArrayList();
AbstractSet c = new HashSet();
System.out.print((a instanceof Collection)+",");
System.out.print((b instanceof Collection)+",");
System.out.print(c instanceof Collection);
}
}
o/p is false true true
Shouldn't line1 give compiler error?Coz HashMap doesn't lie in Collection inheritance hirarchy?
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Veena,
The reason is because Collection is an interface.
Consider the code below:

This code above will compile(but will print false). Why? Because it is possible that a subclass of A would implement the interface I, as in the code below:

Now this code will print true.
Hope this helps.
[ September 14, 2003: Message edited by: Alton Hernandez ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Shouldn't line1 give compiler error?Coz HashMap doesn't lie in Collection inheritance hirarchy?

Check the API: HashMap is a subclass of AbstractMap. Similarly, ArrayList is a subclass of AbstractList and HashSet is a subclass of AbstractSet.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Alton Hernandez:
Hi Veena,
The reason is because Collection is an interface.
Consider the code below:

This code above will compile(but will print false). Why? Because it is possible that a subclass of A would implement the interface I, as in the code below:

Now this code will print true.
Hope this helps.
[ September 14, 2003: Message edited by: Alton Hernandez ]

Alton,
Can I say that , "since Collection extends Object & HashMap extends AbstractMap which inturn extends Object " ,they all lie in the inheritance hirarchy?If this is the case then there is no question of getting compiler error when you are using instanceof operator on Java API classes. Am I right? Please correct me if I am wrong.
Thanks
Veena
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
Hi Veena,
I am not quite sure what your doubt is, but for this line
AbstractMap a = new HashMap(); //line1
to compile, the only requirement would be that HashMap is a subclass of AbstractMap, which is the case so the code does compile.
Hope this helps,
Lalitha Chandran
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443

Originally posted by Veena Point:

Alton,
Can I say that , "since Collection extends Object & HashMap extends AbstractMap which inturn extends Object " ,they all lie in the inheritance hirarchy?

Hi Veena,
I don't think that is the right explanation. The code below would not compile even though A extends Object and B extends Object.

For instanceof to compile properly, the reference(operand1) and the classs type(operand2) must be in the same lineage. And in the example above, A and B are not.

But in the case of an interface, it is different. The compiler has to allow casting of an interface to a class and vice-versa, even though no obvious relationship exists between the two, because down the line, the subclass may still implement that interface.
If you look at my first sample code, even though class A and interface I has no relationship, it is still possible to do this although it will turn out as an invalid casting.

The compiler has no idea if the subclass of A will implement the interface I, as in this case:

If the compile would not allow it, then this will fail, although the casting of a to I, and i to A is valid.
[ September 14, 2003: Message edited by: Alton Hernandez ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
This thread is related to this topic:
http://www.coderanch.com/t/242698/java-programmer-SCJP/certification/Doug-book


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Very well explained Alton
Easier to remember : sibling classes cannot be cast, there must be a father-son relationship or the compiler will complain.


SCJP2. Please Indent your code using UBB Code
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
I got it alton .Thanks .
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Veena,
I am sure excellent explaination by Alton is sufficient for you. However, I just wanted to point out an obvious fact, in case if it was not pointed out already.
Your quote
Alton,
Can I say that , "since Collection extends Object & HashMap extends AbstractMap which inturn extends Object " ,they all lie in the inheritance hirarchy?

Collection is an interface. Interfaces do not extend Object class. All classes by default extend Object class.
Thanks
Barkat
 
Consider Paul's rocket mass heater.
 
subject: using instanceof in Collection framework
 
Similar Threads
instanceof question
Map Question
LinkedHashMap and HashMap
Collecion question
Dan's Question