• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Inherited variable changed . Is that change is visible in superclass

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when a variable is inherited by a subclass.
then object of super class have its own copy of that variable likewise subclass also have its own copy of that variable.Therefore changes made to inherited variable either by subclass or superclass should not be visible to other class.

Then why in line 9 : joly is printed not Java since we are referring to the s of superclass.

 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The confusion lies in the assumption that:

"then object of super class have its own copy of that variable likewise subclass also have its own copy of that variable."

Where did you read that? Your code only proved that statement wrong.
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunny Bhandari wrote:The confusion lies in the assumption that:

"then object of super class have its own copy of that variable likewise subclass also have its own copy of that variable."

Where did you read that? Your code only proved that statement wrong.



If this statement is false when why the following code will print java?
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does not print Java because you are printing the variable s of an object whose state (meaning variable s) you are not changing through your program statement xi.s = "joly"; .
Please note that the above program creates 2 objects with the default value for the instance variable s as Java. Your program changes that default value for just one of those objects which is NOT being printed by your program.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not a static field, so each instance of the class has it's own copy of the variable (which explains the behaviour in the later listing). But it's not true to say that the subclass and superclass instance have separate copies. When you instantiate a subclass there is only one object. It inherits the variable from the superclass, but there's only one copy of it.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic