• 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

Inheritance Hierarchy??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Consider the following code,


Please help me guys on the commented line over the code.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
...to whose x it is pointing?? Is it pointing to the inherited x or the one in the super class...


A main method would demonstrate the behavior...
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call me silly, but I'm getting "non static variable this cannot be referenced from a static context" when I add your main method.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

Where is the question of static here??
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"this" just refers to the instance object itself.
so "this.x" is the "x" defined in Superclass. Inner inherits x from Superclass.


You have to use "TopLevelClass.this.x" if you want to use "x" defined in the enclosing class "TopLevelClass".
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok my question is,

In normal hierarchy(without any superclasses), like as below,

class SuperClass{
int x;
}
class SubClass{
SubClass(){
this.x; //means the one inherited
super.x;//means the one from the super class
}
}

So why is that when it comes to nested classes, this.x refers to the one in the superclass??
Anyone on this??
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Stann:
Call me silly, but I'm getting "non static variable this cannot be referenced from a static context" when I add your main method.



[ November 13, 2006: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
... So why is that when it comes to nested classes, this.x refers to the one in the superclass?? ...


I think you're letting the nesting confuse matters. As Song pointed out above, Inner extends Superclass. Superclass has a variable x, and Inner inherits this variable.

Now, if Inner defined its own variable 'x', then you would see a distinction between this.x and super.x.
[ November 13, 2006: Message edited by: marc weber ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Mark,

so if inner defined x then this.x would mean accessing it's own x and super.x means accessing it's superclass x...Am I right...Since, the variable x is not defined by the inner class, this.x or super.x means the same...i.e accessing the inherited variable x.

I would be greatful if you could confirm this.

Thanks in advance.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
...I would be greatful if you could confirm this...


Exactly! Try this code, which includes both nested and non-nested examples. The behavior is the same.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

My computer crashed and I have to wait for a couple of days untill I get my hard drive replaced and that was why, I have been posting theoretical questions rather than compiling and checking the result for myself.

Thanks a lot mark for the generous help.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
... My computer crashed and I have to wait for a couple of days untill I get my hard drive replaced...


Wow, sorry to hear that. The output for the above code is...

x: Subclass
this.x: Subclass
super.x: Superclass
x: Inner
this.x: Inner
super.x: Superclass
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again mark. I'm understanding the concept behind it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic