• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Casting problem

 
Ranch Hand
Posts: 58
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic