Inheritance of subclasses - please help

Discussion in 'Java' started by Dobieslaw Wroblewski, Jan 29, 2005.

  1. 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.
     
    Dobieslaw Wroblewski, Jan 29, 2005
    #1
    1. Advertising

  2. Dobieslaw Wroblewski

    Bob Guest

    Dobieslaw Wroblewski wrote:

    > 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?)
    --
    Bob
     
    Bob, Jan 29, 2005
    #2
    1. Advertising

  3. Dobieslaw Wroblewski

    Chris Uppal Guest

    Bob wrote:

    > (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/groups?threadm=
    a genuine link this time ;-).

    -- chris
     
    Chris Uppal, Jan 29, 2005
    #3
  4. Hi, thanks both of you.

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

    DW.
     
    Dobieslaw Wroblewski, Jan 29, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    565
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    708
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,824
    Mike Hewson
    Jan 7, 2005
  4. Tony Johansson
    Replies:
    1
    Views:
    300
    Chris Theis
    May 26, 2005
  5. ryanoasis
    Replies:
    5
    Views:
    2,622
    Ian Collins
    May 30, 2007
Loading...

Share This Page