If I have a subclass that has methods I've overridden from the parent class, and under very specific situations I want to use the original methods, how do I call those methods?
call super
class A {
int foo () { return 2; }
}
class B extends A {
boolean someCondition;
public B(boolean b) { someCondition = b; }
int foo () {
if(someCondition) return super.foo();
return 3;
}
}
That's what super
is for. If you override method method
, then you might implement it like this:
protected void method() {
if (special_conditions()) {
super.method();
} else {
// do your thing
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With