Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhinav Yadav
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Inder Kumar Rathore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic