I have two classes A and B. B extends A. Now, in B, can I create an array list of objects of A.
public class A {
// class fields
// class methods
}
import java.util.*;
public class B extends A {
List<Object> listname=new ArrayList<Object>();
A obj=new A();
listname.add(obj);
}
Can I create an array list of objects at all ? By the way, above code gives error !
Yes, I see no reason you cannot create an ArrayList of an object A.
But you can’t do it the way you are doing it, you must do it in a method. You’re trying to do it in the field declarations.
Try maybe adding it in the constructor?
So something like
Or maybe I just don’t understand your question and I’m completely wrong.