• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic