-2

If a user-defined class inherits from the Java's ArrayList<E> class, is it appropriate to have a data field of type ArrayList<E> as a data field? Explain why or why not.

Luke
  • 145
  • 1
  • 2
  • 12

2 Answers2

1

Yes you can do it without any problem but there is a difference between using something as a data member and inheriting a class. We generally inherit when we want to change some functionality of the inherited class but we directly use the objects of class if we want to use the class as it is. Here you have extended ArrayList class so there is no problem in this and you have created a data member too. So you can as you have inherited override the methods of ArrayList class and change them as per your needs and thus these new changes will be reflected in your new data member but extending the class is useless untill you don't override it's method or reuse it. So hope I have made myself clear. Using same data member is no problem but changes made by child class in parent class will be reflected in parent class object.

Mohd Akbar
  • 111
  • 5
0

Why not?

class <T> Xxxx extends ArrayList<T>
{
    private ArrayList<T> another;

      Etc.

}

But normally a member would would be defined with the interface List

I would be more concerned why you are extending ArrayList instead of having it as a member.

tdugan
  • 121
  • 8