How to call parent method from subclass (two level up) ?

Hi.

I have a problem ... I want to call a parent method from subclass(two level up) and(or) directly from object. Below my example ...

--

class A {

protected int var = 1;

public String method() {

return "A";

}

}

class B extends A {

protected int var = 2;

@Override

public String method() {

return "B";

}

}

class C extends B {

protected int var = 3;

@Override

public String method() {

return "C";

}

public String otherMethod() {

return ((A)this).method();

}

}

/** Main class. */

public class Main {

public static void main(String[] args) {

C c = new C();

System.out.println("c.var = " + c.var);

System.out.println("((A)c).var = " + ((A)c).var);

System.out.println("c.method() = " + c.method());

System.out.println("((A)c).method() = " + ((A)c).method());//Polymorphism - will print C

System.out.println("c.otherMethod = " + c.otherMethod());//Polymorphism - will print C

}

}

--

Output:

c.var = 3

((A)c).var = 1

c.method() = C

((A)c).method() = C

c.otherMethod = C

--

I want that two last lines call "A.method()", something like ((A)c).var. Does it even possible? If yes, then how to do it?

Cheers,

Marek

[1392 byte] By [MarekKa] at [2007-11-26 20:30:19]
# 1
Not possible. But show us a real case where you would need it. (Hint: real classes don't have names like "A".)
DrClapa at 2007-7-10 1:20:01 > top of Java-index,Java Essentials,Java Programming...
# 2

Normally you don't want to do that! But if you must, try this:

class A {

protected int var = 1;

public String method() {

return methodImplA();

}

protected final String methodImplA() {

return "A";

}

}

class B extends A {

protected int var = 2;

@Override

public String method() {

return "B";

}

}

class C extends B {

protected int var = 3;

@Override

public String method() {

return "C";

}

public String otherMethod() {

return methodImplA();

}

}

/** Main class. */

public class Main {

public static void main(String[] args) {

C c = new C();

System.out.println("c.var = " + c.var);

System.out.println("((A)c).var = " + ((A)c).var);

System.out.println("c.method() = " + c.method());

System.out.println("((A)c).method() = " + ((A)c).method());//Polymorphism - will print C

System.out.println("c.otherMethod = " + c.otherMethod());//Polymorphism - will print C

}

}

DrLaszloJamfa at 2007-7-10 1:20:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Well I have a situation similar to this:

class A {

@Override

public String toString() {

return "A";

}

}

class B extends A {

@Override

public String toString() {

return "B";

}

}

/** Main class. */

public class Main {

public static void main(String[] args) {

A a = new B();

System.out.println("a = " + a);

}

}

and I wanted to call Object.toString() on "a" just for debug.

At the end I simply log a.getClass() and Integer.toHexString(a.hashCode()) :-)

MarekKa at 2007-7-10 1:20:01 > top of Java-index,Java Essentials,Java Programming...