• 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

Question 11 in Bates and Sierra book

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so this confuses me. I think they are wrong about this question, because when the super() is called, it uses Minor's static int y that was inherited, but then when it gets up to Uber, Uber's constructor changes ITS version of static int y, and doesn't have anything to do with Minor's y. I think that it will print "5" at the end. Am I wrong? Please explain this to me.
 
Dan Silva
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I just don't understand constructors very well. So, if a call to super() is made, then whatever code is in the super-constructor applies to the original constructor's variables? But what if a variable that's used in a super-constructor is marked private, and the subclass doesn't inherit it?
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi First of all tell let me remined you that y is static variable here..and you know that static belongs to class..!!
So both super class and subclass with share the same copy..!!!

I think now if you try to understand the question, you will get to know the flow..!! if you still find any problem ..
We are here to help you..!!
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by Dan Silva:
I guess I just don't understand constructors very well. So, if a call to super() is made, then whatever code is in the super-constructor applies to the original constructor's variables?


Yes


But what if a variable that's used in a super-constructor is marked private, and the subclass doesn't inherit it?


Then, its value is not directly used by the subclass, but might be used by the methods inherited.

You just don't know how the inherited methods works behind the scene.



displays: 10

Regards,
Alex
[ January 23, 2008: Message edited by: Alex Belisle Turcot ]
 
Dan Silva
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What!?!?! The static variable is shared by both classes? Does that mean that I change the subclass version of the static variable, the superclass changes too? I still don't understand.
 
reply
    Bookmark Topic Watch Topic
  • New Topic