How to use the same add () method in two different classes

Hi, is there a way to use the same add () method (adding objects to an array) between two different classes? For example, my lake class has the following add () method:

 public void add (Fish aCatchableThing) 
    {
        if (numThings < catchableThings.length)
        {
            catchableThings[numThings++] = aCatchableThing;
        }
    }

and I'm trying to get it to work with the following code:

public class FishingTestProgram3 
{
    public static void main(String [] args) 
    {

        Lake   weirdLake = new Lake(21);
        weirdLake.add(new AuroraTrout(76, 6.1f));
        weirdLake.add(new Tire());
        weirdLake.add(new Perch(32, 0.4f));
        weirdLake.add(new Bass(20, 0.9f));
        weirdLake.add(new Treasure());
        weirdLake.add(new Perch(30, 0.4f));
        weirdLake.add(new AtlanticWhiteFish(140, 7.4f));
        weirdLake.add(new RustyChain());
        weirdLake.add(new Bass(15, 0.3f));
        weirdLake.add(new Tire());

this will work (using inheritance, i.e. EndangeredFish extends fish, and Perch extends EndangeredFish )for all added fish, but it doesn’t work with objects (for example, Tire, RustyChain, Treasure). Tire, RustyChain and Treasure are classes that extend the SunkenObject class, it’s literally empty:

public abstract class SunkenObject
{


}

I tried to create a second adding method in the lake class, but that didn't work. I was wondering if anyone has any ideas on how this might work? where I could add all things (i.e. fish and sunkenobjects) to the same array so that when

public void listAllThings() 
    {
        System.out.println("  " + this + " as follows:");
        for (int i=0; i<numThings; i++)
        {
            System.out.println("    " + catchableThings[i]);
        System.out.println();
        }

. .

- , add(),

public void add (SunkenObject sunkenObject) 
    {
        if (numThings < catchableThings.length)
        {
            catchableThings[numThings++] = sunkenObject;
        }
    }
+3
3

. , , - Fish. , , - . .

Fish SunkenObject CatchableThing CatchableThing.

add()

public void add(CatchableThing aCatchableThing) 
+5

, . ICatchable:

public void add(ICatchable item)
{
    // Add the catchable item to the lake.
}
+2

, , .

0

All Articles