0

What is the answer?

Q: Class B inherits from class A. The constructor of class A displays "a", while the class B constructor displays "b". What is output to the screen when the code A a = new B(); is run? but why is the output is "ab" instead of "b" ?

27th Feb 2017, 11:13 AM
jiajun goh
jiajun goh - avatar
4 Respuestas
+ 4
Both constructors are called.When B is contructed also constructor from A is called. This is because B inherites from A.
27th Feb 2017, 1:04 PM
Thomas Zenglein
Thomas Zenglein - avatar
+ 1
the answer is "ab"
13th Mar 2017, 12:34 AM
Vira Yanchuk
Vira Yanchuk - avatar
0
object initialisation and destruction are chained (like a stack): each constructor calls a super (parent) constructor BEFORE executing each destructor calls a super (parent) destructor AFTER executing This assures that - by the time we execute our constructor all our parent data is initialised - by the time we execute the Object (parent) destructor all child resources are freed A (parent) <= B <= C Constructor code execution order : A, B, C Destructor code execution order: C, B, A
15th Mar 2017, 1:38 PM
Javier Diaz Soto
Javier Diaz Soto - avatar
0
ys, cause b will inherent all a and will add to it his own
16th Mar 2017, 2:41 PM
Hussain Al-Umran
Hussain Al-Umran - avatar