• 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

using instanceof in Collection framework

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread is related to this topic:
https://coderanch.com/t/242698/java-programmer-SCJP/certification/Doug-book
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it alton .Thanks .
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic