Default Method In Java 8: A Simple Definition with Examples

Prior to Java 8, interfaces could only have abstract methods. These methods implementation must be provided in a separate class. As a result, if a new method is added to an interface, its implementation code must be provided in the class that implements the same interface.

To address this issue, Java 8 introduced the concept of default methods, which allows interfaces to have methods that implement the interface without compromising the classes that implement the interface.

Important points to be noted:

  • Java allows you to add default methods within the interface. Default methods are methods that are declared inside the interface and are tagged with default. These are non-abstract techniques.
  • This feature was provided for backward compatibility so that existing interfaces can take advantage of Java 8’s lambda expression functionality.
  • Methods with a body are referred to as default methods. The most common application of default methods in interfaces is to extend the functionality of a given type without breaking down the implementing classes.

1. Interface without default methods:

package java8;

interface Interface1 {
	void method1();
}
public class DefaultMethod implements Interface1{
	@Override
	public void method1() {
		System.out.println("Method1");
	}
	public static void main(String[] args) {
		DefaultMethod dm = new DefaultMethod();
		dm.method1();
	}	
}

Output :

Method1

The methods defined within the interface must be implemented whenever the interface is used in the preceding program. This is a pre-existing feature.

2. Interface with the default method:

interface Interface1 {
	void method1();
	default void method2() {};
}
public class DefaultMethod implements Interface1{

	@Override
	public void method1() {
		System.out.println("Method1");
		
	}
	public static void main(String[] args) {
		DefaultMethod dm = new DefaultMethod();
		dm.method1();
	}
	
}

Output :

Method1

Take a look at the example above, where we utilised two ways, one of which being the default. Method2() is not implemented in the implemented class because it is specified with the term default. This is a feature that comes standard with the Java 8 implementation.

Let’s look at a different scenario for a moment.

3. Default Method with Static Methods :

interface Interface1 {
	static void method1() {
		System.out.println("Static Method 1");
	}
	default void method2() {};
}

public class DefaultMethod implements Interface1{

	public static void main(String[] args) {
		Interface1.method1();
	}
	
}

Output :

Static Method 1

Let’s take another look at a different scenario.

In the preceding scenario, we have two distinct methods: method1() is the default method, and method2() is a static method. According to the Java core standard, the implemented class is not required to inherit these methods in either case. These methods can also be accessed through class-level objects.

Consider a scenario in which there are numerous default methods with the same name.

4. Default Method with Multiple Inheritance :

interface Interface1 {
	 void method1() ;
	default void method2() {
		System.out.println("Default Method1");
	};
}
interface Interface2 {
	 void method1() ;
	default void method2() {
		System.out.println("Default Method2");
	};
}
public class DefaultMethod implements Interface1,Interface2{

	public static void main(String[] args) {
		DefaultMethod dm = new DefaultMethod();
		dm.method1();
		dm.method2();
	}
	@Override
	public void method2() {
		Interface1.super.method2();
	}
	@Override
	public void method1() {
		System.out.println("Method1");
	}
}

Output :

Method1
Default Method1

In the preceding example. Method2() is overridden in the implemented class. If we do not override the method2() system, an error will be generated. However, this is the default method. Then explain why the system expects the method to be overridden.

Because the compiler searches for a method2 when accessing an object.

However, two methods were discovered because we implemented two interfaces. As a result, it is necessary to specify which will be implemented. That is why, in this case, the override is required.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *