Thursday, 15 May 2014

java - A inherits from B, B inherits from C. Will initializing A invoke constructor of C? -



java - A inherits from B, B inherits from C. Will initializing A invoke constructor of C? -

as preparing interview oop, came across question.

a inherits form b. b inherits c. initializing invoke constructor of c?

what know constructed not inherited. have utilize super keyword refer parent's constructor. right?

the constructor of subclass first invoke constructor of super class. if class has several ancestors, these calls stacked until constructor of top ancestor called. then, go on executing constructor of next ancestors until constructor of ancestors executed. can infer here every time create object, constructor of object class invoked, always.

note if don't add together phone call super() in subclass constructor, compiler add together automatically. if there's no default constructor super class , don't specify phone call specific constructor of super class in constructor of subclass, compiler error.

all these rules stated in jls.

java oop constructor

No comments:

Post a Comment