Java Interview Question Part - 2

Q1: What is Functional Interface ?
An interface which has Abstract Method can be called as Functional Interface Runnable,Comparator amd cloneable are same of the
example for Functional Intrafce,Lambda expressions can only be applied to abstract method of functional interface.

let see the some example bellow:

LambdaExample

Q2: Can we declaring our own Function Interfaces?
We can declare our own Fictional Interface by define Single Abstract Interface Method .We can used @FunctionalInterface  annotation ,to tell compile time errors, it is optional

Function Interfaces


Fuctional interface can has more then one abstract method but we can add more the default method,this Default method has
introduce in Java 8

Default Method

Q3: Why we needed the Default method?
Default methods in interfaces help us to introduce new functionality without breaking the contract of the implementing classes. Default method are those methods in interface which have body and used default keyword.
Let’s see in explanation:
Suppose we have an calculator interface that has addition, substation, percentage etc. as existing implementations.If we want to added new method in division.This can be done with default and static methods without breaking any functionality.


No comments: