File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using instanceof in Collection framework

 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is related to this topic:
http://www.coderanch.com/t/242698/java-programmer-SCJP/certification/Doug-book
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very well explained Alton
Easier to remember : sibling classes cannot be cast, there must be a father-son relationship or the compiler will complain.
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it alton .Thanks .
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic