If I have an abstract class in java named Foo and it has an implementor named Bar then I want to know the following.
lets say Foo looks something like
public abstract class Foo {
Service serviceFoo
...
}
And Bar is
public class Bar extends Foo {
...
}
Also, lets assume I have an instance with Foo, named foo, currently that has serviceFoo instantiated
If I then declare:
Foo foo = new Bar();
will this create a a new instance of Bar that has serviceFoo instantiated or not? E.g. will that field be inherited and instantiated or just inherited?
When you call
new Bar();, the constructor forFoois called implicitly. IfFoo‘s constructor instantiatesserviceFoothen so willBar. IfFoorelies on someone else to instantiateserviceFoothenBarwill do the same.Existing instances of either
FooorBarhave no bearing on what goes on when a new instance is made. Only what code gets executed in the constructor (or what gets passed in as a parameter) has an impact on the new object.