Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "partial" override

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
  }

}
like image 339
Jla Avatar asked Jun 09 '10 14:06

Jla


2 Answers

public class A extends B{

  @Override
  public void foo(){
    System.out.println("yep");
    super.foo(); // calls the method implemented in B
  }  
}
like image 200
Andreas Dolk Avatar answered Oct 13 '22 00:10

Andreas Dolk


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.

like image 23
jjnguy Avatar answered Oct 13 '22 00:10

jjnguy