0

Say I have the function:

public Set<String> giveUp()
{
    Set<String> alreadyGuessed = guessed;
    guessed = new LinkedSet<String>();

    //fill Guessed with possible words
    anag(currentWord, "");

    //Remove ones already guessed
    Iterator<String> alGuessIterator = alreadyGuessed.iterator();
    while (!alGuessIterator.done())
    {
        guessed.remove(alGuessIterator.get());
        alGuessIterator.advance();
    }

    return guessed;
}

When I call this function and try to store it using a line like:

LinkedSet<String> notGuessed = (LinkedSet<String>)wordGame.giveUp();

Will this always be safe regardless of the internal implementation of the function above? In otherwords, could notGuessed be an ArraySet and it still maintain a perfectly safe cast? Or am I misunderstanding the point of the interface being returned and I am just supposed to have "Set notGuessed" to prevent the need for casting?

My teacher is useless on the matter of questions regarding the class, and will also immediately give me a 0 should I do any unsafe casting.

madth3
  • 7,275
  • 12
  • 50
  • 74
Patrick
  • 43
  • 6
  • "My teacher is useless on the matter of questions regarding the class" - That is a bit rude. What do you think would happen if he read this??? – Stephen C Nov 06 '12 at 00:59

5 Answers5

2

That would not be safe, since you can't be sure of the underlying Type. If you just need to access the methods defined by the Set interface, then you should use:

Set<String> set = wordGame.giveUp();

If this happens to be a LinkedSet your code would "work", but if not you will get a ClassCastException. If you need it to be a LinkedSet specifically for any reason, then the giveUp() method should return a LinkedSet explicitly.

doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • Awesome. Glad to see that at least one of my 2 theories was right. Thank you for providing the Exception thrown. Noted for future. – Patrick Nov 06 '12 at 00:40
0

No, your cast will fail with ClasscastException unless return type is exact same as the type you are casting.

Read this post to understand why program to an interface

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
0

No, you are returning Set and casting to LinkedSet.

If you change

Set<String> alreadyGuessed = guessed;
guessed = new LinkedSet<String>();

to

Set<String> alreadyGuessed = guessed;
guessed = new HashSet<String>();

you will have a classcastexception.

A linked set is a Set, but a set is not necessarily a linkedset. Unless you are using methods specific to Linkedset in your code,just remove the cast.

thedayofcondor
  • 3,860
  • 1
  • 19
  • 28
0

You would be far better served not using global fields for things that are only used locally. In this case, you should declare guessed locally, then have anag return something.

Keeping things in the correct scope is sometimes annoying, but prevents the exact sort of confusion you're dealing with here.

As far as the actual problem you asked about, have the method return a LinkedSet, then you don't have to worry about casting.

durron597
  • 31,968
  • 17
  • 99
  • 158
  • Thanks for the input... but completely irrelevant to the question. And just so you know, this is between a GUI and a class. It is meant to be out of scope between each other. – Patrick Nov 06 '12 at 00:43
  • Give up shouldn't be doing the job of creating more guesses! Each method should have one task. To do more complicated tasks, build up more methods. A method should do exactly what it's name says it does. This way, when debugging, you can read a method call and say "oh, I know what that did" - but when you write code where methods do more than that, you have no idea what anything does and get confused, as above. – durron597 Nov 06 '12 at 00:46
0

The whole idea of returning the interface reference type is that all users should be happy with the methods it provides and not worry about the underlying implementation. If you have to cast, you're doing it wrong.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Yes, hence my asking of this question if I could just return the interface. I didn't realize that Java allows just declaring an interface would be allowed when getting it from a return statement in a method. – Patrick Nov 06 '12 at 00:42