By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

Multiple extending

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

PlasmaDragon wrote:
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?


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.println("Something in Y.");
}
}
- --- Z.java ---
public class Z extends Y {
public Z() {
super();
}

public void doSomething() {
System.out.println("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)

iD8DBQFAf1HenwjA8LryK2IRAn+AAKDX6P45FSgv9wDYZTzYat bbl4lVfwCbBX8n
3pX+QgK84LSyM/zNvyAienQ=
=Ab5x
-----END PGP SIGNATURE-----
Jul 17 '05 #2

P: n/a
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.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.