An interface is a contract
(or a protocol, or a common understanding) of what the classes can do.
When a class implements a
certain interface, it promises to provide implementation to all the abstract
methods declared in the interface.
Interface defines a set of
common behaviors. The classes implement the interface agree to these behaviors
and provide their own implementation to the behaviors.
One of the main usage of
interface is provide a communication contract between two objects. If you know
a class implements an interface, then you know that class contains concrete
implementations of the methods declared in that interface, and you are
guaranteed to be able to invoke these methods safely. In other words, two
objects can communicate based on the contract defined in the interface, instead
of their specific implementation.
Java does not support
multiple inheritance (whereas C++ does). Multiple inheritance permits you to
derive a subclass from more than one direct superclass. This poses a problem if
two direct super classes have conflicting implementations. (Which one to follow
in the subclass?). However, multiple inheritance does have its place. Java does
this by permitting you to "implements" more than one interfaces (but
you can only "extends" from a single superclass). Since interfaces
contain only abstract methods without actual implementation, no conflict can
arise among the multiple interfaces.
If a subclass implements two
interfaces with conflicting constants, the compiler will flag out a compilation
error.
the most important use for
interfaces is to reduce coupling between components in your software.
An interface allows to
represent an agreement between classes on how they will talk to each other
without being tied to the actual implementations.
Interface can have static and
default method (Java8).
Note - If a class implements
two interface having default method than it's mandatory to provide
implementation of default common method otherwise compiler will throw compile
time error.
10. If any class in the
hierarchy has a method with same signature, then default methods become
irrelevant. A default method cannot override a method from java.lang.Object.
The reasoning is very simple, it’s because Object is the base class for all the
java classes. So even if we have Object class methods defined as default
methods in interfaces, it will be useless because Object class method will
always be used. That’s why to avoid confusion, we can’t have default methods
that are overriding Object class methods.
11. Java interface default
methods are also referred to as Defender Methods or Virtual extension methods.
Code Snippet-
/**
* Below interface contains default and static
method
* @author Manoj
*/
public interface Test {
default void
print(String str) {
if (!isNull(str))
System.out.println("MyData
Print::" + str);
}
static boolean
isNull(String str) {
System.out.println("Interface
Null Check");
return str == null ? true : "".equals(str) ? true : false;
}
}
Important points about java interface static method:
1. Java interface static
method is part of interface, we can’t use it for implementation class objects.
2. Java interface static
methods are good for providing utility methods, for example null check, collection
sorting etc.
3. Java interface static
method helps us in providing security by not allowing implementation classes to
override them.
4. We can’t define interface
static method for Object class methods, we will get compiler error as “This
static method cannot hide the instance method from Object”. This is because
it’s not allowed in java, since Object is the base class for all the classes
and we can’t have one class level static method and another instance method
with same signature.
5. We can use java interface
static methods to remove utility classes such as Collections and move all of its
static methods to the corresponding interface that would be easy to find and
use.
Functional
Inerface-
I would like to provide a
brief introduction to Functional interfaces. An interface with exactly one
abstract method is known as Functional Interface.
A new annotation
@FunctionalInterface has been introduced to mark an interface as Functional
Interface. @FunctionalInterface annotation is a facility to avoid accidental
addition of abstract methods in the functional interfaces. It’s optional but
good practice to use it.
Functional interfaces are
long awaited and much sought out feature of Java 8 because it enables us to use
lambda expressions to instantiate them. A new package java.util.function with
bunch of functional interfaces are added to provide target types for lambda
expressions and method references.
Functional interface have single abstract
method although functional interface can have multiple default methods.
Code snippet:-
@FunctionalInterface
public interface
FunctionalInterfaceTest{
void getTest();
}
If we add more than one
abstract method it will give compile time error.
@FunctionalInterface
public interface FunctionalInterfaceTest{
void
getTest();
void
getTestTwo();//Invalid '@FunctionalInterface' annotation;
FunctionalInterfaceTest is not a functional interface FunctionalInterfaceTest.java
}
Runnable, Comparator,Cloneable are some of the
examples for Functional Interface
Finctional interface can have multiple static
and default method.
@FunctionalInterface
public interface FunctionalInterfaceTest
{
void getTest();
default void
getTestDefault() {
//default method body
}
default void
getDefaultTest() {
//default method body
}
static void getMethodOne()
{
//static method body
}
static void
getMethodTwo() {
//static method body
}
}
Private and
Private static method:
We can’t use private method in interface so
for but Java SE9 introduces private and private static method in interface to
reduce the code redundancy.
These private method will be accessible to
from other public method of interface.
Refer below code snippet:-
public interface
Java9InterfaceTest {
public static void
getPrivateStaticData(){
getPrivateStaticTest();
}
public void getPrivateData(){
getPrivateTest();
}
private void getPrivateTest(){
//method implementation...
}
private static void getPrivateStaticTest(){
//method implementation...
}
}
Nice blog ..
ReplyDeleteNice information, very usefull thanks
ReplyDeletethanks
ReplyDelete