this is a program from ExamLab question 2 of Test no 2
the program ourputs
"DemoC#"
here is the program
here in this case even if we are doing super.s, then why is the output DemoC# Devaka sir has written the explanation but I am unable to understand that
please help
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
this is so may be because of the object hiding since s is initialized in parent as well as child class so its the s of child class which is being called and not of parent
As I understand it, YClass inherits the variable s. So there is just one variable and it dosen't matter how do you call it, result will always be "C#". It would be another story if the variable would be static.
As there is no variable s in Yclass, so your statement {s="c#";} is equivalent to {super.s="c#"}
this is what compiler does internally.
Remember the tricky statement "Every instance member(field/member func.) must be called by a reference of an object.
Thank you all for your replies
I think I got the point
means
s is the only variable in the program and in the YClass we are assiging the value "C#" to it
but as s is from super class
hence super.s = "C#"
I got it
thanks Martin and Inder Kumar
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
What if i say, it is because of instance initializers. No?
It is inherited by the sub-class and when the default constructor will run it will also make the
instance initializers to run as well, thats why the output is DemoC#.
Sorry if its wrong explanation, because i am warming up again for the preparation.