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
You can'tWell, 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;
1/24/2008 11:28:02 PM
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
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
just have a property in b that has it's parent value as super.xaccess 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