• 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

instance variables

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are instance variables inherited? If yes, does a subclass have a local copy of the instance variable thats declared in superclass?



This program prints: -dabc

Can someone please explain this code?? Thanks a lot.
 
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a constructor is called, on the first line super() is called implicitly. There is just one exception. If the first line is this(...) or super(...), then super() is not called. Considering this information:

In main the constructor of C is called.
From C's constructor, B's constructor is called.
From B's constructor, A() is called.
From A(), A("d") is called.
A("d") makes s="-d"
A() makes s="-da"
B() makes s="-dab"
C() makes s="-dabc"
main prints "-dabc"
 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



At first i want to say that you should start from beginning.It's important to know java code conventions.Way you coded , is it really easy to read ?


Are instance variables inherited? If yes, does a subclass have a local copy of the instance variable thats declared in superclass?



It is depend on Access modifier applied on instance variable in superclass.You need sound study of Member access modifiers topic.It's look small topic but it is a big and confusing topic.gud luck
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes instance variables are accessible on Sub class but as said based on access modifier , private instance variable are not accessible at all.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javin Paul wrote:
, private instance variable are not accessible at all.



Pauly you are right but there is also one case, about you should know that's if default access modifier is applied on superclass instance variable and subclass is not in same package then it'll not be accessible at all in subclass.
 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Riza Aktunc wrote:When a constructor is called, on the first line super() is called implicitly. There is just one exception. If the first line is this(...) or super(...), then super() is not called. Considering this information:

In main the constructor of C is called.
From C's constructor, B's constructor is called.
From B's constructor, A() is called.
From A(), A("d") is called.
A("d") makes s="-d"
A() makes s="-da"
B() makes s="-dab"
C() makes s="-dabc"
main prints "-dabc"



Riza, I know how constructor chaining works, my question was whether a subclass gets a local copy of the variable in superclass.
 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Nandwana wrote:

At first i want to say that you should start from beginning.It's important to know java code conventions.Way you coded , is it really easy to read ?



I didnt write this code! This is exam question!!!

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An instance of the subclass is an instance of the superclass. So yes, it has all the variables that the superclass has (although they may not all be visible to it).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Swiss wrote:I didnt write this code! This is exam question!!!


A real exam question? Or from a mock/book?

If it's the latter, then you should quote the source. If it's the former, then posting it isn't allowed at all - it's against all the terms of the examination. Can you clarify, please? Thanks.
 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




In this line, "B() { super(); s += "b"; } " does class B have a local copy of s? Since, instance variables are inherited, s should refer to B's local copy of s.

And also on this line, "C() { s += "c"; } " s should refer to class C's local (inherited) copy of s.

But since the answer is -dabc, it seems all the changes are applied on the copy of s in superclass.




This question is from the K&B mock exam CD that comes with the textbook.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole concept of "local copy" that you keep repeating is a red herring. For there to be any of kind of copy assumes that there are two separate instantiations: one for the superclass and one for the extending class, and of course, this is not the case.

When an object is instantiated, only a single object, containing all the members declared in the class whether inherited or explicit, is created.

 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok since we are saying "new C()" only C's object is created and because of constructor chaining, it changes the value of s thats declared in superclass A...

Thank you!!
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic