tags:

views:

159

answers:

7

When overriding a method in Java is it possible to call the "original" one. For example:

public class A extends B{

  @Override
  public void foo(){
    System.out.println("yep");
    // Then execute foo() as it's defined in B
  }

}
+10  A: 
public class A extends B{

  @Override
  public void foo(){
    System.out.println("yep");
    super.foo(); // calls the method implemented in B
  }  
}
Andreas_D
The tick goes to the fast gun. For some reason I had misunderstood the use of super
DrDro
@DrDro: you might not have misunderstood it, because this is not the only use of `super`.
Joachim Sauer
+1  A: 

You can call

super.foo();
Daniel Engmann
+1  A: 

You are looking for super.foo().

dpb
+2  A: 

The use of the Keywork super is meant for this

super.foo();
Michael B.
+1  A: 

Try this:

super.foo()
Georgy Bolyuba
A: 

Yep, here's the associated section of the java tutorials

Angelo Genovese
This would be a a good answer if it gave the actual answer **and** linked to the appropriate tutorial.
Joachim Sauer
+6  A: 

Simply call super.methodName() to call your supertype's version of the method.

public class A extends B{
  @Override
  public void foo(){
    System.out.println("yep");
    super.foo(); // Here you call the supertype's foo()
  }
}

Also, this isn't 'partially' overriding the method. You are fully overriding it, but you are just using some of the parent's functionality.

jjnguy