• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Inherited variable changed . Is that change is visible in superclass

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic