I currently have 2 classes with the following structure (simplified here):
class A
{
private List<String> myList = new ArrayList<>();
public A()
{
init();
}
public void init()
{
myList.add("apple");
}
}
class B extends A
{
private List<String> myList = new ArrayList<>();
public B()
{
super();
init();
}
@Override
public void init()
{
myList.add("apple");
}
}
I observe that in A's init(), myList is of size 0, so it was able to add "apple". But in B's init(), myList is null, and it throws a NPE when trying to add "apple".
I know this might be something fundamental that I can't understand and I'd want to wrap my head around this.
Thanks in advance.
Edit: Happened to see this Why child private field is null when printing it from parent class using polymorphism in java? Is it dynamic binding? I have explicitly declared my ctor, moreover it's a list, don't they get automatically initiallized to empty lists?