• 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

Very Interesting question!!!!!

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All!!

while working on one of topic posted in the forum..i end up with very tricky question...here it is..

what will be the output of the below program!!!

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think- "HarMol","uk","uk"
 
kamal shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.......Just copy this program and run this on your machine you will find something very interesting!!!
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya you are right! its "HarMol", "uk", "dub"!
Ok i got it, here jume() is called on refernce "hs" which updates its objects's "nat" from "dub" to "uk" value. Hence hs.nat shows "uk".

But "this" refers to the "hm" which originally called go() and hence its showing its nat (this.nat) as the initial value "dub"
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



what would be the o/P??
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The O/p is :

HarMol
uk
dub
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt get it

Firstly I thought there would be compile time error for "nat=UK" as
I thought that's behaving like a local variable which is not declared (ie String).

Can someone explain in detail what's happening there..and what's being updated and how.
 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do not consider the commented lines.
 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the below code.



what is the o/p?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I couldn't get how the value of "this.nat" is "dub" ....

please elaborate on this some more...

Thanks in advance...

Varsha
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought it would be

HarMol
uk123
uk123

But the output is
HarMol
dub
uk123

How does inheritance influence the instant variable?
Does the child class replace it with a new value or are they separate variables.

Also if we are creating a child class object with the reference of the Parent class ,why is it acessing the parent class variable,
unlike (overriding) method which will execute the child class method.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nabila,

Instance variables are inherited but they are not polymorphic. Polymorphic invocations are only for overridden instance methods. Not for variables, not for static methods.
This is explained in K&B, Chapt 2. Polymorphism, last para.

So since HelSte hs is a reference to parent type,
hs.nat will be dub.

this.nat be uk123 since 'this' is hm.

If you say
HarMol hs = new HarMol();
hs.nat will be uk123

Hope this helps,
Sheryl.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.That helps alot.
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic