Multiple extending

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PlasmaDragon

    Multiple extending

    I have a class, let's call it "x". I have another class, y, which
    extends x. Then I have a class z which extends y. There is a
    function in y (but not in x) which I have overridden in z. But when I
    try to run the program, it uses the version of the function in y! I
    have heard that java doesn't support multiple inheritance, so could
    this be the problem?

    So basicall: is there a rule in java which I am violating by trying to
    have a class extend a class extending a class? And if so, what can I
    do to work around it?
  • Chris

    #2
    Re: Multiple extending

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    PlasmaDragon wrote:
    [color=blue]
    > I have a class, let's call it "x". I have another class, y, which
    > extends x. Then I have a class z which extends y. There is a
    > function in y (but not in x) which I have overridden in z. But when
    > I
    > try to run the program, it uses the version of the function in y! I
    > have heard that java doesn't support multiple inheritance, so could
    > this be the problem?
    >
    > So basicall: is there a rule in java which I am violating by trying
    > to
    > have a class extend a class extending a class? And if so, what can
    > I do to work around it?[/color]

    Hi,
    No rules violated here, and this doesn't actually have to do with
    multiple inheritance. Multiple inheritance means one class cannot
    *directly* extend two classes. However, I'm not sure where your
    problem comes from. Examine the following code, of my design:

    - --- X.java ---
    public class X {
    public X() {
    }
    }


    - --- Y.java ---
    public class Y extends X {
    public Y() {
    super();
    }

    public void doSomething() {
    System.out.prin tln("Something in Y.");
    }
    }


    - --- Z.java ---
    public class Z extends Y {
    public Z() {
    super();
    }

    public void doSomething() {
    System.out.prin tln("Something in Z.");
    }

    public static void main(final String[] args) {
    Y y;
    Z z;

    y = new Y();
    y.doSomething() ;

    z = new Z();
    z.doSomething() ;

    y = z;
    y.doSomething() ;
    }
    }


    - --- END OF FILES ---

    Compiling the above files, then running as "java Z", produced the
    expected output of:

    Something in Y.
    Something in Z.
    Something in Z.

    - --
    Chris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQFAf1Henwj A8LryK2IRAn+AAK DX6P45FSgv9wDYZ TzYatbbl4lVfwCb BX8n
    3pX+QgK84LSyM/zNvyAienQ=
    =Ab5x
    -----END PGP SIGNATURE-----

    Comment

    • PlasmaDragon

      #3
      Re: Multiple extending

      I found the problem. The function I overrode in Z was called in Y by
      another function, so it used the Y version. So I copied the caller
      function into Z and the problem is solved.

      Comment

      Working...