Java Default MethodLast Updated : 12 Jan 2026 In Java, default methods were introduced in Java 8. Default methods are the methods that are defined in an interface with concrete implementation. It allows us to define method bodies directly inside interfaces. These methods are non-abstract methods. Prior to Java 8, only abstract methods were allowed in the interface. It means that implementing classes were responsible for providing the method's body. Default methods are defined using the default keyword before the method's return type. Why Default Methods?Before Java 8:
With Default Methods:
Example: Default MethodExampleCompile and RunOutput: Greetings! Hello from default method! Multiple Inheritance ConflictIf a class implements two interfaces with the same default method, it must override that method to resolve ambiguity. For example, consider the following program. In the above program, we have defined two interfaces A and B. Both having the show() default method. The main class C implements both the interfaces A and B, which causes method ambiguity since Java does not know which show() to inherit. To resolve this ambiguity, C overrides the show() method and explicitly calls one of the super-interface versions using A.super.show() or B.super.show(). If A.super.show() invoked, we get A's default as output. If B.super.show() invoked, we get B's default as output. Characteristics of Default MethodOverriding: Implementing classes can choose to use the default implementation or override it to provide their own specific behavior. Multiple Inheritance of Behavior: Default methods allow for a form of multiple inheritance of behavior, as a class can implement multiple interfaces, each potentially containing default methods. This differs from traditional class inheritance, which only allows single inheritance. Conflict Resolution: If a class implements multiple interfaces that define default methods with the same signature (the "Diamond Problem"), the implementing class must explicitly override the conflicting method to resolve the ambiguity. It can also explicitly call a specific interface's default method using InterfaceName.super.methodName(). Backward Compatibility: They enable the addition of new methods to existing interfaces without forcing all implementing classes to immediately provide an implementation, thus preventing breakage of existing codebases. For example, the forEach method added to the Iterable interface in Java 8 is a default method. Code Reusability: They allow interfaces to provide a default behavior for methods, reducing code duplication in implementing classes that might otherwise provide the same boilerplate implementation. Static Methods inside Java 8 InterfaceWe can also define static methods inside the interface. Static methods are used to define utility methods. The following example explains, how to implement static method in interface. ExampleCompile and RunOutput: Hello, this is default method. This is abstract method. This is static method. Abstract Class Vs. Java 8 InterfaceAfter having default and static methods inside the interface, we think about the need of abstract class in Java. An interface and an abstract class are almost similar except that you can create constructor in the abstract class whereas you cannot do this in interface. For example, consider the following program. ExampleCompile and RunOutput: You can create constructor in abstract class Addition: 30 Subtraction: 10 Multiplication: 200 Next TopicJava 8 forEach() Method |
We request you to subscribe our newsletter for upcoming updates.