Updated February 10, 2023
Introduction to Java 8 Default Methods
Java 8 default methods are created within interfaces, and provide this type of functionality. Earlier Java 8 interfaces only contained abstract methods. Another class implements these methods. So we need a new method in the interface, and then the interface implementation code is provided into the class for implementing the specified interface; java 8 provides default methods for this issue.
Key Takeaways
- Java 8 introduced the concept of default methods; the default method is a method that contains the body.
- The use of the default method in the interface adds functionality to the type that was given to implement the class. The default method is also known as the defender method.
Overview of Java 8 Default Methods
The default methods allows us to implement interfaces without affecting any classes. The methods that were defined within interfaces and tagged as default are referred to as default methods in Java. This method is also known as the non-abstract method. The default methods feature includes backward compatibility, so we can use the old interfaces to leverage the lambda expression capability.
For example collection of list interfaces do not contain the declaration of forEach method; same time, if we add the default method, then it will break the implementation of the collection framework. Java 8 introduced default methods because the collection and list interfaces contain their default implementation by using forEach method.
How to Use Default Methods in Java 8?
Default methods, also known as defender methods, allow us to add new methods to existing interfaces. The below syntax shows how we can use the default methods in java as follows:
Syntax:
public interface name_of_interface
{
default void method_name()
{
// code
}
}
We can see in the above syntax that we are creating the default method under the public interface, which means that this method will not interfere with the class that we have already created.
Below example shows how we can create the default method as follows:
Code:
interface DmethodInt
{
public void sq (int s);
default void show ()
{
System.out.println ("Executed");
}
}
class Dmethod implements DmethodInt
{
public void sq (int s)
{
System.out.println (s*s);
}
public static void main(String[] args)
{
Dmethod dm = new Dmethod ();
dm.sq (6);
dm.show ();
}
}
Output:
The below example shows how we can use multiple interfaces by using default methods. If implemented interface contains the default method by using the same signature, then we need to specify the class by overriding the default method.
Code:
interface Tint1
{
default void show ()
{
System.out.println ("Default Tint1");
}
}
interface Tint2
{
default void show ()
{
System.out.println ("Default Tint2");
}
}
class Dmethod implements Tint1, Tint2
{
public void show()
{
Tint1.super.show ();
Tint2.super.show ();
}
public static void main(String[] args)
{
Dmethod dm = new Dmethod ();
dm.show ();
}
}
Output:
Why Use Java 8 Default Methods?
We are using the default method to solve the issue of backward compatibility, so we are using existing lambda expressions without implementing any methods into the class which was implemented.
The default method provides the feature of the interface of concrete methods; we can consider it as correction in java. At the time core API will build in java, the developer is using interfaces which were defining the API interface like the API of java collections. By using java 8 streams, developers wanted to modify the API to provide support for the streams. If we want to add new methods to the java interface, we can add the same by using default methods. They are also providing backward compatibility by providing pre-existing implementation of default methods.
To achieve this objective developer is using the concept of default method in interfaces. We are deciding where we are implementing the default method for using default implementation.
The below example shows why we are using default methods as follows:
Code:
interface p
{
default void pa ()
{
System.out.println ("Logic of default parsing");
}
}
class TextParser implements p {
}
public class Dmethod implements p
{
@Override
public void pa ()
{
System.out.println ("XML file parsing");
}
public static void main(String[] args)
{
p par = new TextParser();
par.pa ();
par = new Dmethod ();
par.pa ();
}
}
Output:
Implementation of Default Methods in Java 8
As the name suggests, it will simply be the default method; if we now override them, then these are the methods which was invoked by the class of caller. We are defining the default methods in interfaces.
The below example shows how we are implementing the default method as follows:
Code:
interface tint
{
public void sq (int s);
default void show()
{
System.out.println("Implementation of default method");
}
}
class Dmethod implements tint
{
public void square (int s)
{
System.out.println (s*s);
}
public static void main(String[] args)
{
Dmethod d = new Dmethod ();
d.square (10);
d.show ();
}
@Override
public void sq(int s) {
}
}
Output:
Method Invocation
We are invoking the default method, as per the perspective of code in the main class; the default methods are nothing but virtual methods. At the time a single class is implementing the interface by using the default method, the main code of the program will invoke the default method, which generates the invoke interface.
Below example shows how we can invoke the default method in java as follows:
We are creating public default method.
Code:
class pclass
{
public void hparent ()
{
System.out.println ("Parent class");
}
}
interface pint
{
public default void h1()
{
System.out.println ("HInterface");
}
}
interface pint1
{
public default void h2 ()
{
System.out.println ("Interface");
}
}
public class Dmethod extends pclass implements pint, pint1 {
public void h3 ()
{
super.hparent ();
pint.super.h1 ();
pint1.super.h2 ();
}
public static void main(String[] args)
{
new Dmethod().h3();
}
}
Output:
Examples
Given below are the examples mentioned:
Example #1
In the below example, we are defining the class name as Dmethod.
Code:
interface int1 {
public void mul (int p);
default void show ()
{
System.out.println ("Multiplication");
}
}
class Dmethod implements int1 {
public void mul (int p)
{
System.out.println (p*p);
}
public static void main(String[] args) {
Dmethod mul = new Dmethod ();
mul.mul (20);
mul.show ();
}
}
Output:
Example #2
In the below example, we are defining multiple inheritance.
Code:
interface T1
{
default void show ()
{
System.out.println ("Default T1");
}
}
interface T2
{
default void show ()
{
System.out.println ("Default T2");
}
}
class Dmethod implements T1, T2
{
public void show()
{
T1.super.show ();
T2.super.show ();
}
public static void main(String args[])
{
Dmethod dm = new Dmethod ();
dm.show ();
}
}
Output:
Conclusion
This method is also known as the non-abstract method. The default methods feature contains backward compatibility, so we can use the old interfaces for leveraging the capability of the lambda expression. It is created inside the interfaces, Java 8 provides this type of facility. Before the version of java 8, interfaces only contain abstract methods.
Recommended Articles
This is a guide to Java 8 Default Methods. Here we discuss the introduction, how to use default methods in java 8, implementations, and examples. You may also have a look at the following articles to learn more –