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.

- 145
- 1
- 2
- 12
-
Possibly related: [What does it mean to “program to an interface”?](https://stackoverflow.com/q/383947) – Pshemo Dec 29 '18 at 01:18
-
5It's not helpful to post your homework here unless you demonstrate an attempt. – nicomp Dec 29 '18 at 01:29
-
Create an interface. – Thorbjørn Ravn Andersen Dec 29 '18 at 01:41
-
What do you mean by "have a data field of type `ArrayList
` as a data field"? Why wouldn't it be appropriate? – Dawood ibn Kareem Dec 29 '18 at 01:47
2 Answers
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.

- 111
- 5
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.

- 121
- 8