• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymorphic generics

 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to make a generic change in a subclass? for example:


if you call go, will it use the subclasses ArrayList? If not, is there any way to use the subclasses ArrayList?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have a grasp on polymorphism.... here's my guess....

I think you need to do it this way....



I just thought that to override something you need to name it the same method name (and have the same parameters) as the superclass. Then if you want to do the superclasses' method too, you need to call the super.methodName(). The subclass has an arraylist because to be an animal means you (excuse the poor grammar) "HAS-A" ArrayList.

 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your right, but that isn't exactly what I was asking, but I wasn't being very clear. So let's say I have a superclass with an arraylist of animals, and a feed() method that cycles through the arraylist and feeds all the animals in it. Then I make a subclass where I have an arraylist of something that extends animals, and is named the same name as the superclasses arraylist. If you call the feed method on my subclass would it uses the subclasses arraylist, or the superclasses arraylist? And if it uses the superclasses, is there any way to make it use the subclasses? I know the obvious solution is to override the feed method, but that seems counterproductive, as it would have the exact same code as the superclasses feed method.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I THINK the answer is to do this:



Maybe we'll both learn something here... maybe I've already learned something. Who knows?
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right of course, thank you for the speedy answers. What I was originally trying to do I'm pretty sure is impossible, but what you did has the same effect (although it does involve more typing, sadly)

Thank you for your time
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should either have a pure superclass without implementation, or if the superclass offers implementation it should be such that the subclasses have use for it.

So either you do,



Above the supertype just states that at a zoo animals are fed. The subtypes implement the rest.

Or you do this,



Here the super type offers an implementation. It offers to keep animals and to handle the feeding. But it lets the subtypes hand over food of the right kind
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Embla is correct. What she didn't say (but I think it was implicit in "supertype offers an implementation") was that your MonkeyZoo does not behave as something which "IS-A" zoo. You have a List<? extends Animal> in one, then you have a List<Monkey> in the other. So you now have two Lists in the subclass. The two private fields do not override each other.

Is it possible to create a class which is Zoo<T extends Animal> whose field is a List<T>?
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I see what you mean, I was trying to override variables,which isn't allowed.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
colton peterson wrote:Okay, I see what you mean, I was trying to override variables,which isn't allowed.
Not so much "not allowed" as "not possible". The private field is regarded as only existing in one class; if you try to create another field with the same name, you end up with two independent and separate fields.
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right, that is what I meant, believe it or not. And thanks for the help, I managed to fix my problem.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done, fixing it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic