Inheritance of subclasses - please help

  • Thread starter Dobieslaw Wroblewski
  • Start date
D

Dobieslaw Wroblewski

I such a code well-formed?

class A0 {
void f(Subcl s) {
}
...
class Subcl {
....
}
}

class A1 extends A0 {
...
class Subcl extends A0.Subcl {
}
}

Does derived function f accept parameters of A1.Subcl and iterpret their
fields/methods (inherited or overriden from A0.Subcl) correctly?

DW.
 
B

Bob

Dobieslaw said:
Does derived function f accept parameters of A1.Subcl and iterpret their
fields/methods (inherited or overriden from A0.Subcl) correctly?


Seemingly not. And it took me a while to understand why.

I used your code to create this file:

/* BEGIN CODE */
class A0 {
A0() {
System.out.println("In A0 constructor");
}

void f(Subcl s) {
System.out.println("In f with " + s);
System.out.println("Inner class instance value is "
+ s.instanceValue);
}

class Subcl {
int instanceValue = 3;

Subcl() {
System.out.println("In A0.Subcl constructor");
}
}
}

class A1 extends A0 {
A1() {
System.out.println("In A1 constructor");
}

class Subcl extends A0.Subcl {
int instanceValue = 6;
int otherValue = 9;

Subcl() {
instanceValue = 6;
System.out.println("In A1.Subcl constructor");
}
}
}

public class InnerClassExtension {
public static void main(String [] args) {
A1 a = new A1();
A1.Subcl s = a.new Subcl();
a.f(s);
System.out.println("s.instanceValue is "
+ s.instanceValue);
System.out.println("s.otherValue is "
+ s.otherValue);
}
}
/* END CODE */


which compiles fine, and the output of running it gives:

In A0 constructor
In A1 constructor
In A0.Subcl constructor
In A1.Subcl constructor
In f with A1$Subcl@194df86
Inner class instance value is 3
s.instanceValue is 6
s.otherValue is 9


So method f(Subcl s) *does* have access to the A1.Subcl instance, but
because A1.Subcl is an extension of A0.Subcl, A1.Subcl also has access
to a superclass instance. (Because a constructor always calls its
superclass constructor first.)

Which means that when you call method f on an instance of A1.Subcl, it
actually refers to the instance variables of the instance of A0.Subcl,
which means that it probably is not accessing the values you were hoping
it would access.

Someone please correct me if I'm wrong. (Anyone recommend a good book on
the perils of inheritance?)
 
C

Chris Uppal

Bob said:
(Anyone recommend a good book on
the perils of inheritance?)

http://www.amazon.com/exec/obidos/ASIN/0852029462/102-4053641-9070507

But the Java Language Specification edition 2 (or 3 if it ever comes out) is
always the last word on the subject.

Inner class instance value is 3
s.instanceValue is 6
s.otherValue is 9


So method f(Subcl s) *does* have access to the A1.Subcl instance, but
because A1.Subcl is an extension of A0.Subcl, A1.Subcl also has access
to a superclass instance. (Because a constructor always calls its
superclass constructor first.)

This is what you'd expect even without introducing nested classes into the mix.
Variables don't override, so it a method on class Base accesses an instance
field of Base, and then Derived declares another field with the same name/type,
the Base method will not "see" the new variable even if the object actually is
a Derived.

For methods, however, the picture is different. Method /do/ override, so if a
method in Base calls another method that is overridden in Derived, it will be
the overriding method that is called (if the object is an instance of Derived,
of course).

Inner/nested classes don't change any of that. An inner class is just a class
with special access to its containing class.

However, its worth noting that the compiler uses a number of fairly complex
hacks to implement the "special access", and the combination of the hacks, plus
inheritance, can sometimes lead to unexpected results (including bugs in the
compiler, for a recent example, see this thread:
http://groups.google.co.uk/[email protected]
a genuine link this time ;-).

-- chris
 
D

Dobieslaw Wroblewski

Hi, thanks both of you.

If f will see A0.Subcl variables and call A1.Subcl methods - this is
perfectly what I want :).

DW.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top