I'm new to Java, so excuse if this is in the wrong group, or off base.
I have two classees
[code]
public class Base {
protected Long id;
protected String name;
...
public Strin getName() {
return this.name;
}
}
public class subBase extends Base {
...
public someOtherMethod() {
}
}
In my code I have this:
ArrayList<subBase> multipleInstance = new ArrayList<subBase>();
multipleInstance = fillSubBase();
public void manipulateArray(ArrayList<Base> arrayToManipulate) {
//This is what I want, but it is wrong
arrayToManipulate.get(0).getName();
}
[/code]
I want to have a base class with a member and method that will be
available to all sub classes. I also want an external method that can
utilize that base class method on all sub classes. I want to write a
method that can use getName on all subclasses so I can write this
method only once. To use the method in the function body, I need the
<base> call, but that disallows me from passing <subBase> to the
method.
Does this make sense? I tried to keep the explanation generic, but
might have made it too confusing. IF so, I will try and re-wright this.
Thanks for your time
cjburkha - 21 Mar 2006 19:35 GMT
Ahh, I missed a line:
This:
ArrayList<subBase> multipleInstance = new ArrayList<subBase>();
multipleInstance = fillSubBase();
public void manipulateArray(ArrayList<Base> arrayToManipulate) {
//This is what I want, but it is wrong
arrayToManipulate.get(0).getName();
}
Should be:
ArrayList<subBase> multipleInstance = new ArrayList<subBase>();
multipleInstance = fillSubBase();
//Added this
manipulateArray(multipleInstance);
public void manipulateArray(ArrayList<Base> arrayToManipulate) {
//This is what I want, but it is wrong
arrayToManipulate.get(0).getName();
}
Oliver Wong - 21 Mar 2006 21:28 GMT
> I'm new to Java, so excuse if this is in the wrong group, or off base.
>
[quoted text clipped - 36 lines]
> Does this make sense? I tried to keep the explanation generic, but
> might have made it too confusing. IF so, I will try and re-wright this.
Try something like:
public void manipulateArray(ArrayList<? extends Base> arrayToManipulate)
- Oliver
cjburkha - 22 Mar 2006 23:10 GMT
Thank you for that tip.
I tried your syntax and it didn't work just like that, but you gave me
a good start, and I will continue down that path.
Thanks again