Java-8 Features ================ 1) Default and static methods in an interface: ============================================== -> In an Interface, we can define only: public static final variables public abstract methods before the java-8. -> But, the java-8 has introduced default methods and static methods also in interface. default methods: ================ interface I1{ void m1(); void m2(); // void m3(); // new method default void m3(){ // default implementation } } class MyClass1 implements I1{ @Override void m1(){ // logic } @Override void m2(){ //logic } } class MyClass2 implements I1{ @Override public void m1(){ // logic } @Override public void m2(){ //logic } @Override public void m3(){ // own logic } } class MyClass3 implements I1{ @Override void m1(){ // logic } @Override void m2(){ //logic } } ==> The abstract method of an interface, can be implemented in separate implemented class. ==> So, if a new method is to be added in an interface, then its implementation code has to be provided in the implemented classes of the same interface. ==> To overcome this issue, Java-8 has introduced "default method" ==> The default method can be define with "default" keyword. Syntax: default void method-name(){ default-implementation } ==> The default method can define with some default implementation within the interface. ==> The Implemented classes of interface can use that default method with default implementation and can also override that default method according required logic if need. ====================================================================== Static method: ============== Ex: interface Remunerators{ void calculateSalary(); // abstract method default void deductHealthPremium(){ // default implementation } static void deductPensionPremium(){ // logic } } class FTEmployee implements Remunerators{ @Override public void calculateSalary(){ // logic } @Override default void deductHealthPremium(){ // new implementation } } class PTEmployee implements Remunerators{ @Override public void calculateSalary(){ // logic } } ->When a method need to implement uniquely on all the implemented classes of the same interface, then that method we can create as "Abstract method". -> When a method need to be implement based on the requirement in the selected implemented class of the interface, that method can make as "default method". -> When a method implementation is same among all the implemented classes of the same interface, we can define that method as "static method". default void deductHealthPremium(){ // default implementation } ================================================== Functional Interface: ===================== -> When an interface with Single Abstract Method (SAM), we can call that as "Functional Interface". -> The functional interface may contain any number of variables like: public static final, default methods and static methods also but it can be with only one SAM. Ex-1: ===== interface MyInter{ void m1(); // abstract method default void m2(){ // default implementation } } -> The above definition is for functional interface because it has only one: abstract method and one default method. Ex-2: ===== interface MyInter{ void m1(); // abstract method default void m2(){ // default implementation } default void m3(){ // default logic } } -> the above definition is also considered for functional interface. Because it has SAM. Ex-3: ===== interface MyInter{ void m1(); // abstract method void m2(); default void m3(){ // default implementation } } -> the above interface is the normal interface because it has more than one abstract method. Ex-4: ===== interface MyInter{ void m1(); // abstract method void m2(); default void m3(){ // default implementation } static void m4(){ // logic } } -> the above definition is not a functional interface. Ex-5: ===== @FunctionalInterface interface MyInter{ void m1(); // abstract method void m2(); default void m3(){ // default implementation } } -> In this case, the compiler can throw an error (compile time error) Ex-6: ===== @FunctionalInterface public interface MyInter{ void m1(); String toString(); default void m2() { // default logic } } -> The above definition is a functional interface definition. Because, toString() is an Object class method. Ex-7: ===== @FunctionalInterface public interface MyInter{ void m1(); Boolean equals(String s); int hashCode(); } -> the above is not a functional interface. Because it has two abstract methods: 1) m1() 2) equals(String s) Ex-8: ===== @FunctionalInterface public interface MyInter{ void m1(); Boolean equals(Object o); int hashCode(); } -> The above is the functional interface definition. Because equals() and hashCode() methods are Object class methods. Pre-defined Functional Interfaces: ================================== 1) Runnable ==> void run() 2) Comparable ==> int compareTo(T t) 3) Comparator ==> int compare(T t) etc.