• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting problem  RSS feed

 
naro pad
Ranch Hand
Posts: 58
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the a question to the following code:



s.subSet("b", true, "d", true) method returns NavigableSet. How this can be cast to a TreeSet? On the hierarchy tree we have TreeSet implements NavigableSet. NavigableSet CAN'T DO what TreeSet CAN.

Any help please...
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naro pad wrote:
I have the a question to the following code:



s.subSet("b", true, "d", true) method returns NavigableSet. How this can be cast to a TreeSet? On the hierarchy tree we have TreeSet implements NavigableSet. NavigableSet CAN'T DO what TreeSet CAN.

Any help please...


You are correct. A TreeSet IS-A NavigableSet, but a NavigableSet is not necessarly a TreeSet. In this case, it works because of an implementation detail. The subSet() method returns a TreeSet as the returned NavigableSet. In other words, casting to a TreeSet works, but isn't guaranteed to always work.

Henry
 
naro pad
Ranch Hand
Posts: 58
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I still don't get it...

On the API it says that subset() returns a NavigableSet<E> which is an INTERFACE. It's not a TreeSet and it can't be a TreeSet. Correct me if I am wrong please.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naro pad wrote:Sorry but I still don't get it...

On the API it says that subset() returns a NavigableSet<E> which is an INTERFACE. It's not a TreeSet and it can't be a TreeSet. Correct me if I am wrong please.


It doesn't have to be a TreeSet. But it might be a TreeSet, because TreeSet implements NavigableSet. It might be any class that implements NavigableSet. That's why the compiler allows you to try and cast it.

At runtime, if it isn't a TreeSet then you'll get a ClassCastException. In this case you don't get one because the TreeSet class has been implemented in a way that means it the object it returns is in fact a TreeSet. However, this is an implementation detail. All the class contract guarantees is that the object returned will implement NavigableSet. Which means that in the future it could change, and this would break code that assumes it is a TreeSet.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naro pad wrote:Sorry but I still don't get it...

On the API it says that subset() returns a NavigableSet<E> which is an INTERFACE. It's not a TreeSet and it can't be a TreeSet. Correct me if I am wrong please.

What would you expect it to return ? It can't return an actual NavigableSet because you can't create instances of interfaces. It has to return an instance of a class that implements NavigableSet and TreeSet is such a class.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could get a little help if you try to view this in terms of simpler case of casting problem. consider this generic case

A B = (C) D

1. C and D should have relationship ie C IS-A D or D IS-A C, otherwise you will get compiler error

2. A and C should be of same type or C should be of subtype of A otherwise compiler error

3. D should refer to object whose TYPE should either be C or subtype of C otherwise runtime error

the 3rd point is important. now subset returns NavigableSet, but as Stuart set you cant create instances of an Interface, so that means this method will return a type that IMPLEMENTS NAVIGABLE SET . now this implementation can be TreeSet(since TreeSet implements NavigableSet) or it cannot be. if it is a TreeSet cast will be success, if it is not a TreeSet or subtype of TreeSet, you will get ClassCastException.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!