0

I've got a class Results which extends ArrayList<Results>. I've got an object i which has a function i.getResults() which returns an ArrayList of type ArrayList<Results>. I'm attempting the following:

Results result = i.getResults();

However, I'm getting a type mismatch error. Why can't I make this assignment? How can I assign it to type Results?

EDIT: To explain what I'm trying to achieve.. I don't have access to the ArrayList<Results> code and I'd like to make it implement a certain class which I have Results implementing

2
  • It is important to have a clear idea of what you are trying to achieve. Can you describe in English what you are really trying to do here. Apr 13, 2011 at 10:39
  • Ok, now imagine you are explaining this to someone non-technical. BTW: ArrayList<Results> is just the code for ArrayList which everyone has access to. Apr 13, 2011 at 12:47

5 Answers 5

3

You can't - ArrayList<Results> isn't a Results. Results is an ArrayList<Results> though, you could assign the other way.

7
  • Well, I need to Implement something onto the type ArrayList<Results> but don't have the code to that class. How would I do this?
    – Skizit
    Apr 13, 2011 at 10:34
  • Make a wrapper class instead of extending.
    – Erik
    Apr 13, 2011 at 10:35
  • One option is to give Results a constructor that takes an ArrayList<Results> Apr 13, 2011 at 10:35
  • Result(Collection<? extends Results> results) { super(results); } would probably do the job. Apr 13, 2011 at 10:47
  • @jeff Are you sure you meant it to extend results? throws back an undefined.
    – Skizit
    Apr 13, 2011 at 10:56
1

You're assigning a parent to the child class. This is not possible. You can assign do

ArrayList<Result> list = new Results();
0
1

You've got the relationship the wrong way round.

class T {}
class S extends T {}

The LSP states that:

in a computer program if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substitutes for objects of type T), without altering any of the desirable properties of that program (correctness, task performed, etc.)

So in other words, anywhere that expects a T can get an S with no problems. Something that wants an S cannot be given a T because it isn't specific enough.

In your example, i.getResults() returns an ArrayList<Results>. You can't assign that to Results because Results is more specific than the array list.

0

You have

  Results extends ArrayList<Results>

So anywhere you might use ArrayList you can use Results, but not vice-versa. When you say extends you are saying I can do everything he can do, and a little bit more.

  C extends D

says: if all you need is a D then you can use either a C or a D. But someone needing a C cannot accept a D, because it doesn have all the capabilities C offers.

0

Well, you're trying to assign an object of a superclass type to a reference of a subclass type, and of course that's a compile-time error, because it might not be valid. It's the same as if you tried to assign the return value of a method that returns Object to a variable of type String. If you are absolutely sure the getResults() method returns a Results object, you should change its return type. Or if you're only sure of it in this context, you can use a cast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.