Link to home
Start Free TrialLog in
Avatar of Case-Sensitive
Case-Sensitive

asked on

Return Types

Hi

i have devised a solution to access a superclass arraylist by returning an arraylist of the current arraylist in the superclass.

Is this god programming, is it allowed, is there any other solutions to access the arraylist from the subclass, withought having a protected field.

This is what i was going to use:

public abstract class A
{
    private ArrayList list1;

    public ArrayList method1()
    {
         return list1;
     }
}

my subclass uses method1 from superclass to access the arraylist from the superclass A and print the contents from it:

public class B extends A
{
    public void printlist1()
    {
        Iterator it = method1().iterator();
        while(it.hasNext())
         {
            system.out.println(it.next());
          }
    }
}

Any help on this subject would be great
Thanks alot
Avatar of petmagdy
petmagdy
Flag of Canada image

Hi Case-Sensitive,

the returned from method is the reference to the object holded by the supperclass so u can read & modify its contents, so yes u can do that

Cheers!
Avatar of Case-Sensitive
Case-Sensitive

ASKER

Hey thanks alot

I was wondering if it is a good idea to inherit an ArrayList from an abstract class, it seems to make the job alot harder when adding to or retreiving from the subclasses.

Should i use an individual ArrayList inside class B or Class C and remove it from class A?

any help on this
Cheers!
ASKER CERTIFIED SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

remember that only extends one class only if you really need it. One java class can only extends one class and no more.

Regards
Dave
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial