Jla
Jla

Reputation: 11384

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
  }

}

Upvotes: 6

Views: 4299

Answers (6)

Andreas Dolk
Andreas Dolk

Reputation: 114797

public class A extends B{

  @Override
  public void foo(){
    System.out.println("yep");
    super.foo(); // calls the method implemented in B
  }  
}

Upvotes: 13

jjnguy
jjnguy

Reputation: 138902

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.

Upvotes: 7

Georgy Bolyuba
Georgy Bolyuba

Reputation: 8541

Try this:

super.foo()

Upvotes: 1

Michael B.
Michael B.

Reputation: 3430

The use of the Keywork super is meant for this

super.foo();

Upvotes: 3

user159088
user159088

Reputation:

You are looking for super.foo().

Upvotes: 2

Daniel Engmann
Daniel Engmann

Reputation: 2850

You can call

super.foo();

Upvotes: 1

Related Questions