wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes polymorphic generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "polymorphic generics" Watch "polymorphic generics" New topic
Author

polymorphic generics

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

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?


www.mormon.org
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

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.


When you do things right, people won't be sure you've done anything at all.
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

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

Joined: Sep 14, 2009
Posts: 1659
    
  11

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

Joined: Nov 18, 2007
Posts: 97

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

Joined: Oct 22, 2009
Posts: 237
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

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: Nov 18, 2007
Posts: 97

Okay, I see what you mean, I was trying to override variables,which isn't allowed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: Nov 18, 2007
Posts: 97

right, that is what I meant, believe it or not. And thanks for the help, I managed to fix my problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Well done, fixing it
 
Don't get me started about those stupid light bulbs.
 
subject: polymorphic generics
 
Similar Threads
Axis - describing arrays in WSDL : "unbounded sequence" instead of "soapenc:Array"
Parent child object problem
Coupling and cohesion
Inheritance and Polymorphism
Super bounded WildCards -Doubt