Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

pass subclass to method that takes base class

Thread view: 
cjburkha - 21 Mar 2006 19:16 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.