Java8::Interfaces
interfaces: Interfaces are used in Java to provide a template to developers .In an interface all the fields (variables) are by default public, static and final. Up to Java 1.7 version, all the methods declared in interfaces are public and abstract by default. Since Java 1.8, an interface can have default methods and static methods as well.
An interface can have default methods and static methods. Any other methods are implicitly public and abstract. All the fields declared in an interface are implicitly public, static and final constants.
interface exampple:
interfaces: Interfaces are used in Java to provide a template to developers .In an interface all the fields (variables) are by default public, static and final. Up to Java 1.7 version, all the methods declared in interfaces are public and abstract by default. Since Java 1.8, an interface can have default methods and static methods as well.
An interface can have default methods and static methods. Any other methods are implicitly public and abstract. All the fields declared in an interface are implicitly public, static and final constants.
interface exampple:
Output: from implementaion class::Register
from default impl from Register
in above code if you comment out defaultimpl in register class,call object of esgiter.defaultimpl((from eight interface method as described above).
Output: from implementaion class::Register
from default impl from eigthinterfac
how can you call static methods used in class implements interface(eight interface): you can call static methods in class or interace using classname:methodname
output : from implementaion class::Register
from default impl from eigthinterface
from display() heloo
How can you call if two interfaces have same methods?
call the default method of the specified interface using super keyword.following is example having two interfaces(eigthinterface,secondinterface) and class register.
call same method using class or interfcename.super.methodname.
OutPut: from implementaion class::Register
from default impl from eigthinterface
from default impl from SecondInterface
from display() heloo
FunctionalInterface::
An interface with one abstract method declaration is known as a functional interface. Functional Interface is also know as SAM Interface because it contains only one abstract method.
SAM Interface stands for Single Abstract Method Interface.
The compiler verifies all interfaces annotated with a
@FunctionalInterface that the interfaces really contain one and only one abstract method.
A compile-time error is generated if the interfaces annotated with this annotation are not functional interfaces. It is also a compile-time error to use this annotation on classes, annotation types, and enums. The FunctionalInterface annotation type is a marker interface.
A functional interface is simply an interface that has exactly one abstract method. The following types of methods in an interface do not count for defining a functional interface:
• Default methods
• Static methods
• Public methods inherited from the Object class
Note that an interface may have more than one abstract method, and can still be a functional interface if all but one of them is a redeclaration of the methods in the Object class.
to define fucntion interface using @Fucntioninterface on interface,below are following rues:
i)Define an interface with one and only one abstract method.
ii)We cannot define more than one abstract method.
iii)Use @FunctionalInterface annotation in interface definition.
iv)We can define any number of other methods like Default methods, Static methods.
v) If we override java.lang.Object class’s method as an abstract method, which does not count as an abstract method.
Note:Even if you are not defining functional interface annotation on interface but should define only one abstract method by default it cosidered as functional interface.
if we apply functional interface in classe it will show following error