Reputation: 1
I am trying to override the methods using a method in which I am using if-else condition. Is it possible in Java to override the methods using method with conditions?
Upvotes: 0
Views: 622
Reputation: 140504
You can't override methods using an if
statement - if
s can only live inside blocks (such as methods), so you either do or don't override a method.
However, you can conditionally invoke the super method inside the overriding method:
@Override void yourMethod() {
if (someCondition) {
super.yourMethod();
} else {
// Do something else.
}
}
Upvotes: 3