I was reading about Predicate Interface in Java 8, got stuck at why Java made negate(), and() & or() as default method and not static method
Default methods can be easily overridden in implementing class. As per my understanding, the behavior of these methods should never be changed.
Predicate also have isEqual() method which is static
I understand the purpose of the default method as mentioned here(Purpose of Default or Defender methods in Java 8) i.e: Not breaking client implementation if we add a new method in the interface, lambda impl
But my ques is why not make them static method instead of the default method? Benefits of making it static:
- No one can override them
- predicate is still Functional Interface (same as default)
- Can have lambda implementations for such an interface (same as default)
Found the answer: If we make these methods static, then we will not be able to chain these methods
p1.or(p2) will not be possible