aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super reference nor referring to the super class Examlab Test no 2 question 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super reference nor referring to the super class Examlab Test no 2 question 2" Watch "super reference nor referring to the super class Examlab Test no 2 question 2" New topic
Author

super reference nor referring to the super class Examlab Test no 2 question 2

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

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
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
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


No Dream Too Big
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
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.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
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.



best of luck...


Thanks

Living is easy with your eyes closed.....
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

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
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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.

Happy Preparation,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: super reference nor referring to the super class Examlab Test no 2 question 2