User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » question about inheritance in Java Page [1]  
umbrellaman
All American
10892 Posts
user info
edit post

Let's say I have three classes; A, B, and C. The inheritance hierarchy is such that B extends A and C extends B (ie A <-- B <-- C). Let us also assume that each class has an int value "x" (which we'll assume is public for all of the classes). Finally, let us assume that I'm working from within the C class.

If I want C's value of x, all I have to do is say "x" or "this.x". If I instead want B's value of x, I have to say "super.x". But what do I do if I want A's value of x? How do I access methods or data from classes that are above the superclass?

1/24/2008 11:19:42 PM

jaZon
All American
27048 Posts
user info
edit post

You can't

Well, not by any normal means. I suppose you may be able to get it to work, but it's not good practice.


Well, take this how you will;

Quote :
"Sub-classes can use super keyword to access the shadowed variables in super-classes. This technique allows for accessing only the immediate super-class. super.super is not valid. But casting the 'this' reference to classes up above the hierarchy will do the trick. By this way, variables in super-classes above any level can be accessed from a sub-class, since variables are resolved at compile time, when we cast the 'this' reference to a super-super-class, the compiler binds the super-super-class variable. But this technique is not possible with methods since methods are resolved always at runtime, and the method gets called depends on the type of object, not the type of reference variable. So it is not at all possible to access a method in a super-super-class from a subclass."


[Edited on January 24, 2008 at 11:32 PM. Reason : ]

1/24/2008 11:28:02 PM

umbrellaman
All American
10892 Posts
user info
edit post

Okay, thanks.

I guess I shouldn't have asked about methods. All I really need to do is access variables.

This is from a homework question, btw. It's an interesting question because I've never thought about multi-level inheritance before. I have something that works, but basically I have a method in C which calls a method in B, which itself then returns the value stored in A. This works, but I had this feeling that this is not how the instructor intended for me to solve the problem.

Thanks again.

1/24/2008 11:38:53 PM

jaZon
All American
27048 Posts
user info
edit post

Yea, sorry I can't be of more help. I thought about this a while back and after listening to a professor ramble on about how it is poor practice I realized it really would be a pain in the ass to even attempt.

1/25/2008 12:11:27 AM

Rat
Suspended
5724 Posts
user info
edit post

just have a property in b that has it's parent value as super.x

access that property, done.


if you aren't propagating enough values down the chain then perhaps you should rethink some design

1/25/2008 10:32:28 AM

 Message Boards » Tech Talk » question about inheritance in Java Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.