Skip to main content

Posts

Showing posts with the label static method in interface

Java 8 - default and static methods in interfaces

Java 8 introduced default and static methods in interfaces. These features allow us to add new functionality in the interfaces without breaking the existing contract for implementing classes. How do we define default and static methods? Default method has default and static method has static keyword in the method signature. public interface InterfaceA { double someMethodA(); default double someDefaultMethodB() { // some default implementation } static void someStaticMethodC() { //helper method implementation } Few important points for default method You can inherit the default method. You can redeclare the default method essentially making it abstract . You can redefine the default method (equivalent to overriding). Why do we need default and static methods? Consider an existing Expression interface with existing implementation like ConstantExpression , BinaryExpression , DivisionExpression and so on. Now, you want to add new functionalit