2

I have class A which has a method sum().

Now class B extends class A and overrides sum() method, and

class C extends class B and also overrides sum() method.

Now I create an instance of class C.

My question is: How can I call class A's sum() method in class C through C instance? One of the way is call super.sum() in B and C. Is there any other way?

RuntimeException
  • 1,201
  • 3
  • 14
  • 25
  • A obj = new C(); then call method obj.sum(); Or use super to call that method – Kick Mar 12 '14 at 06:51
  • No, as per accepted answer of http://stackoverflow.com/questions/3456177/calling-super-super-class-method – Scary Wombat Mar 12 '14 at 07:00
  • Where you have created an instance of class C ? – Sudz Mar 12 '14 at 07:03
  • @Sudz in main method of Main class – RuntimeException Mar 12 '14 at 07:07
  • First you will have to call that method from the class `B` using `super.sum()` and create another method in your class `B` and from that method class `B` method call the super class `A` method and in your class `C` you call the class `B` method by creating instance of class `C`. This way it will first call first the class `B` method in which there is already called first class `A` method. This way it can work. – GrIsHu Mar 12 '14 at 07:11
  • @GrIsHu can you represent your answer programmatically? – RuntimeException Mar 12 '14 at 07:12
  • @RuntimeException You can refer the answer of "Seelenvirtuose" for that. – GrIsHu Mar 12 '14 at 07:22

4 Answers4

1

Inside B's & C's sum method place super() so that it will call A's sum method.

B extends A{
sum(){
super();
//dosomething..
}
}
Shriram
  • 4,343
  • 8
  • 37
  • 64
1

you can call A's sum in B using super.sum() but i don't think u can do that from C.

The best way would be to call super.sum() in both B's anc C's sum

Raghvendra Singh
  • 1,775
  • 4
  • 26
  • 53
1

you can try like this :

class A{
    public void method() {
        System.out.println("a");
    }
}
class B extends A {
    public void method() {
        System.out.println("b");
    }
    public void callmethod(){
        super.method();
    }
}
class C extends B {
    public void method() {
        System.out.println("c");
    }
    public void callmethod(){
        super.callmethod();
    }
}
public class Test {

public static void main(String[] args) {
    C c = new C();
    c.callmethod();
}

}

AKHIL K
  • 84
  • 1
  • 2
  • 11
1

There is no syntactical way of calling the method A.sum() inside any method of C. You need to place a bridging method inside B for doing that:

public class A {
    public void sum() { System.out.println("A.sum()"); }
}

public class B extends A {
    @Override
    public void sum() { System.out.println("B.sum()"); }
    protected final void superSum() { super.sum(); }
}

public class C extends B {
    @Override
    public void sum() { System.out.println("C.sum()"); }
    public void someMethod() {
        sum();
        super.sum();
        superSum();
    }
}

Calling C.someMethd() prints out

C.sum()
B.sum()
A.sum()
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66