Jump to content

Using an inherited method in another class' method

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Hi, I have a Class Manager which extends Class Person.

Person has a method called getName().

I also have another Class, Workplace, which stores an array of Managers.

In yet another Class, I have stored an array of Workplaces each with an array of Managers, and now have to access the getName() method of all Managers in the array, ie produce a list of their names, in THIS class. I'm trying to use this:
return workplaces[i].managers.getName();

However, I get an error saying that the method getName() cannot be found in the Manager class. But, since Manager inherits from Person, shouldn't it be able to use the getName() method on any manager object? The variables of Person (i.e name) is protected and so is usable by Manager, and the getName() method is public.

Can anyone point out where I'm going wrong?

Edited by ZipOnTrousers, 13 November 2009 - 07:56 AM.


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I'd have each workplace have a "getManagerNames()" method that returns a String of all the names of the managers. However, if this isn't good enough, you can use a double nested loop, like so:
StringBuilder bld = new StringBuilder();
for (int iii = 0; iii < workplaces.length; ++iii)
{
    for (int jjj = 0; jjj < workplaces[iii].managers.length; ++jjj)
    {
        bld.append("\n" + workplaces[iii].managers[jjj].getName());
    }
}
return bld.toString();
Simply having return workplaces[noparse][/noparse].managers.getName(); won't work because getName() isn't a method of an array. Remember that Arrays are objects like anything else in Java, so they have methods and properties (such as [i]length). You'll need to select which manager in the managers array before using the getName() method.
Wow I changed my sig!