Updated February 10, 2023
Introduction to Java 8 Interface
Java 8 Interface is nothing but an abstract method which includes a collection of functions and constant variables. This is the most important core concept in Java, and it is used to accomplish polymorphism, abstraction, and multiple inheritances. The essential benefits of using the Java 8 Interface are the capability to create new concrete method and also it includes the new methods of concrete functions to the existing interface which has been implemented already without affecting the functionality of a program.
Key Takeaways
- Testing in java is simple: Java interface makes unit testing easier.
- Flexibility: It increases flexibility, the result with more flexible code.
- Fast development: Interface does not require any implementation, so the only thing is to agree name of the interface and operation on them which is easy compared to coding the operations.
- Loosely couple code: It develops with loosely coupled code that is simple to alter and maintain code without affecting the entire structure.
- Dependency injection: It is based on the interface, framework itself injects dependency which is achieved by declaring dependencies as interface.
What is Java 8 Interface?
Java 8 Interface is an essential procedure in Java Programming development, it supports providing the functional program, API’s manipulation for data and time, JavaScript Engine and latest Streaming API, and so on. Java 8 Interface is an abstract method which includes the collection of functions and constant variables.
Java 8 Interface Changes
In Java 8 Interface there was a change in which they introduced methods in interfaces, which are static method and default Methods. With the help of the Default Methods in Interface developers can be able to include more methods to the interfaces. By this method, it won’t affect or there will not be any modifications in the class which they implement the interface.
In Java 8 Interface, there are of two methods called Static and Default methods in interface, initially, we are having only the abstract methods in interface. Currently in Java 8 we can be able to define the Static and Default methods in Java and also it enables to make use of the Lambda Expressions with the interface functionalities in Java.
While designing an interface it is complicated to do a task because if we like to include the additional functionalities in Interface it affects the entire implementing class, so we require to modify the entire implementation process. When interface go older the number of implementation classes also grows to extent, at last, it is complicate to extend the interfaces. So that while designing an application itself mostly the frameworks offer the base implementation class and then we can able to extend it and also override the methods which are applicable for the application.
Default, Static Method
In Java 8 it introduced the default methods which provides backward compatibility so that the existing interface makes use of the Lambda Expressions without any changes of implementing the methods in the classes while implementation. The name of default methods is also called the virtual extension methods or the defender methods.
1. Default Method
In Java, it offers the service to build the Interface within the default methods. The functionalities that are well-defined within the interface then tags with the default which is called as the default methods and these methods are called by the non-abstract methods. Let’s see the examples of both the methods Default and Static; initially, we begin with the Default method. In this example, the interface function called Sayable contains the abstract and the default method. Default method is used to describe the method with the default implementation; we can override the default method to offer the particular implementation for the method.
Code:
interface Sayable
{ // here is the Default method
default void say()
{
System.out.println("This is the Default Method");
}
void sayMore(String msg);
}
public class DefaultMethods implements Sayable{
public void sayMore(String msg)
{
System.out.println(msg);
}
public static void main(String[] args) {
DefaultMethods dmethod = new DefaultMethods();
dmethod.say(); // make a call to the default method
dmethod.sayMore("Work-Worship"); // abstract method call
}
}
Output:
2. Static Method
In this we can also define the static functionality inside the interface it helps to describe the utility methods.
Let’s see the below sample coding which describes the flow of execution of the static method in interface.
Code:
interface Sayable{
// default method
default void say(){
System.out.println("\nHello Everyone\n");
}
// Abstract method
void sayMore(String msg);
// static method
static void sayLouder(String msg){
System.out.println(msg);
}
}
public class StaticMethods implements Sayable{
public void sayMore(String msg)
{ // abstract implementation
System.out.println(msg);
}
public static void main(String[] args) {
StaticMethods smethod = new StaticMethods();
smethod.say();// making call to -default method
smethod.sayMore("\nWork-Worship\n");
// to make a call to abstract method
Sayable.sayLouder("Welcome...");
// making call to static- method
}
}
Output:
Functional
Functional Interfaces were introduced in Java 8. This functional interface will be implemented using the annotation called the FunctionalInterface. It makes sure that the interfaces have only the interface abstract method; this keyword is optional as the method defined inside the interface default by abstract. The functional interface has several default methods but with only abstract method.
In Functional Interface there are four major things they are introduced in Java 8, that will be used in various formats.
- Consumer
- Predicate
- Function
- Supplier
Multiple Inheritances
Java allows multiple inheritances by using the interface; Interface selects the default implementations for their functionalities, which means the class implements the multiple interfaces which defines the methods with same signature and the child class inherits the individual implementations. In Java 8, the interface selects the default implementation for their methods the interface still defines the abstract methods. It explains the class implements the multiple interfaces which defines the methods with the same signature.
Interface contains the methods and variables like class but the methods in interface are abstract by default. Multiple inheritances by interface occur if the class implements multiple interfaces or the interface extends the multiple interfaces.
Code:
interface HospitalFood {
void Food();
}
interface HospitalTravel {
void Travel ();
}
class Patients implements HospitalFood, HospitalTravel {
public void Food() {
System.out.println("Hospital Patients are Eating");
}
public void Travel () {
System.out.println("Hospital Van Travelling");
}
}
public class SampleDemo {
public static void main(String args[]) {
Patients Obj = new Patients ();
Obj.Food();
Obj.Travel();
}
}
Output:
Examples of Java 8 Interface
The functional interface introduces with Java 8 will obtain the arguments by the object of Type T and it returns the object of Type R, the arguments and the output are of different type.
Example #1
Code:
import java.util.function.Function;
public class FunctionDemo {
public static void main(String[] args) {
// sample code-1
Function < String, Integer > function = (t) -> t.length();
System.out.println(function.apply("Tansha"));
}
}
Output:
Example #2
Code:
import java.util.function.Function;
public class FunctionDemo
{
public static void main(String[] args)
{
// sample code-2
Function < Integer, String > function2 = (_no ) -> {
if (_no % 2 == 0) {
return "Given Number " + _no + " is even";
} else {
return "Given Number " + _no + " is odd";
}
};
System.out.println(function2.apply(17));
}
}
Output:
FAQs
Given below are the FAQs mentioned:
Q1. Whether multiple inheritance applicable?
Answer: Java supports multiple inheritances by interfaces that is the class implements two or more interfaces. It introduced default methods so that the ambiguity problem arises because both of interface with the same signature and with the same method.
Q2. Is it possible to invoke the interface default method when implementing two interfaces?
Answer: Yes, with use of Super Keyword.
Eg: <interfaceName>super<defaultMethodName>
Q3. Can we access the static methods inside the interface?
Answer: Yes, with the help of Interface name.
Eg: <interfaceName>,<variableName>
Conclusion
In this article we have learned about the Java interface, it is a blueprint of class which contains abstract methods and static constants. It helps to achieve abstraction and polymorphism. It has the capability of achieving new concrete method which includes the methods of concrete functions to exist without affecting the functionalities.
Recommended Articles
This is a guide to Java 8 Interface. Here we discuss java 8 interface changes, default, static method, multiple inheritances & examples. You can also look at the following articles to learn more –