Tuesday, August 9, 2016

When java 8 has default method in interface, do we still need abstract class

The constraint on the default method is that it can be implemented only in the terms of calls to other interface methods, with no reference to a particular implementation's state.

The interface can have fields. But all fields in an interface are implicitly public, static and final which means it can be used as a state because it cannot be changed.

All methods in an interface are implicitly public and abstract, but not final.

The conclusion is that if the method does not need a state and does not need to call private methods, use interface default method, otherwise using an abstract class.

No comments:

Post a Comment