pass subclass to method that takes base class

C

cjburkha

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();
}
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
 
C

cjburkha

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();
}
 
O

Oliver Wong

cjburkha said:
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();
}
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.

Try something like:

public void manipulateArray(ArrayList<? extends Base> arrayToManipulate)

- Oliver
 
C

cjburkha

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top