• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question about inheritance...

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it prints : B.s1 A.s2 A.s1 A.s2
I thought it printed: B.s1 A.s2 B.s1 A.s2
why?
Is variable s1 in class B hiding s1 in class A? how's the procedure to figure this out.. when you extend a class are you inheriting all non-private members (instance variables, methods)?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Andres,

Is variable s1 in class B hiding s1 in class A


yes it is.

when you extend a class are you inheriting all non-private members


yes. except those which have hided any super class members.

how's the procedure to figure this out..


only instance methods are overriden.
Super s = new Sub();
s.m(); // will call sub's m IF it is overriden.
on the other hand feilds are shadowed. feilds are accessed using the reference variable type.
Super s = new Sub();// ref var is Super
s.aField; // will access S's aField
Sub s = new Sub();
Super sup = (Sup)s;
sup.aField; // will still access S's aField because the variable 'sup' reference type is Sup and not Sub.
In order to access Super's hidden feilds through Sub refernece, u can 'cast' to Super.
Sub s = new Sub();
int aField = ((Sup)s).aField;// now will access Sup's even if it is shadowed in Sub.
hope it helps.
[ June 07, 2003: Message edited by: G Nadeem ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
You can refer to JLS 8.3.3.1
And no need for cast to refer to the hidden field,just use "super." prefix (from non-static method).
Alexan
[ June 07, 2003: Message edited by: Alexan Kahkejian ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is variable s1 in class B hiding s1 in class A?


Well, technically speaking, the term should be shadowing.


how's the procedure to figure this out..


For shadowed variables, you look at the reference, not the object pointed to by the reference.


when you extend a class are you inheriting all non-private members (instance variables, methods)?


yes
 
G Nadeem
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton,

Well, technically speaking, the term should be shadowing.


If a class declares a field with a certain name , then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in super classes,and superinterfaces of the class

JLS 2Ed 8.3 pp153-154

A declaration d of a type named n shadows the declarations of any other types named n that are in scope at the point where d occures throughout the scope of d

JLS 2Ed 6.3.1 p86.
so i think in above case is that of hiding and not shadowing. am i making any mistake here?..
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by G Nadeem:
so i think in above case is that of hiding and not shadowing. am i making any mistake here?..


Yes, you're right, its hiding. My mistake


(JLS 6.3.1)
Hiding, in the technical sense defined in this specification, applies only to members which would otherwise be inherited but are not because of a declaration in a subclass.

 
The only thing that kept the leeches off of me was this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic