YusafShayk
YusafShayk

Reputation: 1

can we override methods using IF else conditions in Java

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

Answers (1)

Andy Turner
Andy Turner

Reputation: 140504

You can't override methods using an if statement - ifs 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

Related Questions