Default Methods In Java 8 Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Before Java 8, interfaces could only have abstract methods. The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface, then its implementation code has to be provided in the class implementing the same interface. To overcome this issue, Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface. Java // A simple program to Test Interface default // methods in java interface TestInterface { // abstract method public void square(int a); // default method default void show() { System.out.println("Default Method Executed"); } } class TestClass implements TestInterface { // implementation of square abstract method public void square(int a) { System.out.println(a*a); } public static void main(String args[]) { TestClass d = new TestClass(); d.square(4); // default method executed d.show(); } } Output: 16 Default Method Executed The default methods were introduced to provide backward compatibility so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods. Static Methods: The interfaces can have static methods as well which is similar to static method of classes. Java // A simple Java program to TestClassnstrate static // methods in java interface TestInterface { // abstract method public void square (int a); // static method static void show() { System.out.println("Static Method Executed"); } } class TestClass implements TestInterface { // Implementation of square abstract method public void square (int a) { System.out.println(a*a); } public static void main(String args[]) { TestClass d = new TestClass(); d.square(4); // Static method executed TestInterface.show(); } } Output: 16 Static Method ExecutedDefault Methods and Multiple Inheritance In case both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method. Java // A simple Java program to demonstrate multiple // inheritance through default methods. interface TestInterface1 { // default method default void show() { System.out.println("Default TestInterface1"); } } interface TestInterface2 { // Default method default void show() { System.out.println("Default TestInterface2"); } } // Implementation class code class TestClass implements TestInterface1, TestInterface2 { // Overriding default show method public void show() { // use super keyword to call the show // method of TestInterface1 interface TestInterface1.super.show(); // use super keyword to call the show // method of TestInterface2 interface TestInterface2.super.show(); } public static void main(String args[]) { TestClass d = new TestClass(); d.show(); } } Output: Default TestInterface1 Default TestInterface2Important Points: Interfaces can have default methods with implementation in Java 8 on later. Interfaces can have static methods as well, similar to static methods in classes. Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code. Comment More infoAdvertise with us Next Article Default Methods In Java 8 A Akash Ojha Improve Article Tags : Java Practice Tags : Java Similar Reads Can We Override Default Method in Java? Default method in Java is a method in java which are defined inside the interface with the keyword default is known as the default method. It is a type of non-abstract method. This method is capable of adding backward capability so that the old interface can grasp the lambda expression capability. J 3 min read Default Array Values in Java If we don't assign values to array elements and try to access them, the compiler does not produce an error as in the case of simple variables. Instead, it assigns values that aren't garbage. Below are the default assigned values. S. No.DatatypeDefault Value1booleanfalse2int03double0.04Stringnull5Use 2 min read Method Class | getDefaultValue() Method in Java The getDefaultValue() method of java.lang.reflect.Method class. It returns the default value for the annotation member represented by the Method object. When annotation member belongs to primitive types, then the method returns the instance of that wrapper type. It returns null if annotation member 3 min read How to Call a Method in Java? In Java, calling a method helps us to reuse code and helps everything be organized. Java methods are just a block of code that does a specific task and gives us the result back. In this article, we are going to learn how to call different types of methods in Java with simple examples.What is a Metho 3 min read Java Interface Methods In Java, an interface is an abstract type used to specify the behaviour of a class. One of the most important rules when working with interfaces is understanding how methods are declared and implemented. In this article, we are going to learn the interface methods in detail.Note: Interface methods a 3 min read Method Class | isDefault() Method in Java The java.lang.reflect.Method.isDefault() method is used to check whether the method of the Method object is a Default Method: or not. It returns true if method object is a default method, otherwise it will return false. Default Method: A public non-abstract on-static method with a body declared in a 2 min read Java Methods Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea 8 min read URL getDefaultPort() method in Java with Examples The getDefaultPort() function of URL class returns the default port of a specified URL. If the URL scheme or the URLStreamHandler for the URL do not define a default port number then the function returns -1. Function Signature public int getDefaultPort() Syntax url.getDefaultPort() Parameter This fu 2 min read Integer doubleValue() Method in Java The doubleValue() method of Integer class of java.lang package is used to convert the value of the given Integer to a Double type after a widening primitive conversion and returns it. Syntax: public double doubleValue() Parameters: The method does not take any parameter. Return Value: This method re 1 min read TimeZone getDefault() Method in Java with Examples The getDefault() method of TimeZone class in Java is used to know the default TimeZone for this system or host. This may vary in according to the implementation in different environment. Syntax: public static TimeZone getDefault() Parameters: The method does not take any parameters. Return Value: Th 1 min read Like