152

I have two Java classes: B, which extends another class A, as follows :

class A {
    public void myMethod() { /* ... */ }
}

class B extends A {
    public void myMethod() { /* Another code */ }
}

I would like to call the A.myMethod() from B.myMethod(). I am coming from the C++ world, and I don't know how to do this basic thing in Java.

Belphegor
  • 4,456
  • 11
  • 34
  • 59
Creasixtine
  • 740
  • 3
  • 11
  • 33

12 Answers12

153

The keyword you're looking for is super. See this guide, for instance.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 5
    Looks like in C++ you can call a specific ancestor method, but not java, you can just pass it up the chain... – rogerdpack Oct 17 '11 at 18:00
145

Just call it using super.

public void myMethod()
{
    // B stuff
    super.myMethod();
    // B stuff
}
pevik
  • 4,523
  • 3
  • 33
  • 44
Robin
  • 24,062
  • 5
  • 49
  • 58
21

super.MyMethod() should be called inside the MyMethod() of the class B. So it should be as follows

class A {
    public void myMethod() { /* ... */ }
}

class B extends A {
    public void myMethod() { 
        super.MyMethod();
        /* Another code */ 
    }
}
Belphegor
  • 4,456
  • 11
  • 34
  • 59
21

Answer is as follows:

super.Mymethod();
super();                // calls base class Superclass constructor.
super(parameter list);          // calls base class parameterized constructor.
super.method();         // calls base class method.
eeerahul
  • 1,629
  • 4
  • 27
  • 38
8

call super.myMethod();

Elie
  • 13,693
  • 23
  • 74
  • 128
7

I am pretty sure that you can do it using Java Reflection mechanism. It is not as straightforward as using super but it gives you more power.

class A
{
    public void myMethod()
    { /* ... */ }
}

class B extends A
{
    public void myMethod()
    {
        super.myMethod(); // calling parent method
    }
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51
4

Use the super keyword.

Steve K
  • 19,408
  • 6
  • 52
  • 50
4
super.baseMethod(params);

call the base methods with super keyword and pass the respective params.

kinshuk4
  • 3,241
  • 3
  • 33
  • 41
3
class test
{
    void message()
    {
        System.out.println("super class");
    }
}

class demo extends test
{
    int z;
    demo(int y)
    {
        super.message();
        z=y;
        System.out.println("re:"+z);
    }
}
class free{
    public static void main(String ar[]){
        demo d=new demo(6);
    }
}
albusshin
  • 3,930
  • 3
  • 29
  • 57
umanathan
  • 1
  • 2
3

See, here you are overriding one of the method of the base class hence if you like to call base class method from inherited class then you have to use super keyword in the same method of the inherited class.

Rahul Yadav
  • 143
  • 6
2
// Using super keyword access parent class variable
class test {
    int is,xs;

    test(int i,int x) {
        is=i;
        xs=x;
        System.out.println("super class:");
    }
}

class demo extends test {
    int z;

    demo(int i,int x,int y) {
        super(i,x);
        z=y;
        System.out.println("re:"+is);
        System.out.println("re:"+xs);
        System.out.println("re:"+z);
    }
}

class free{
    public static void main(String ar[]){
        demo d=new demo(4,5,6);
    }
}
Belphegor
  • 4,456
  • 11
  • 34
  • 59
umanathan
  • 1
  • 2
1

If u r using these methods for initialization then use constructors of class A and pass super keyword inside the constructor of class B.

Or if you want to call a method of super class from the subclass method then you have to use super keyword inside the subclass method like : super.myMethod();