Java 8 Concepts and Understanding..1
why Java8 → To achieve conciseness in the code (compact code or to shorten Line Of Code) by using Lambda expression.Few years back Java face a great loss in market because some language like Python,scale etc give you same result in much less LOC,hence to prevent the losses java introduce Functional programming and upgraded itself.
what are the new features → Lambda exp,Stream API, Default methods, static methods in interface ,Functional Interface,optional class,Date API,Method reference ,Nashorn Javascript engine etc.
Lambda Exp →It is a anonymous fuction( without name,return type, and access modifier but have symbol “->”. example
we replace method
public void add(int a,int b){System.out.println(a+b)}
as
(a,b)-> System.out.println(a+b) in lambda exp using functional interface.
Functional Interface → it has only one abstract method,any no of static and default methods. example Runnable interface,Comparable etc. they are use to provide the reference to lambda Exp for ex: we can write
Comparator<String> c=(s1,s2)-> s1.compareTo(s2);
here (s1,s2)-> s1.compareTo(s2) → this is lambda exp,
Comparator<String> c →Functional Interface
Create new Functional interface → 1.create an Interface 2.Annotate that with @FunctionalInterface 3.Define only one Abstract method. 4.use any no of static and default methods.
@FunctionalInterface
public interface MyFunctionalInterface{
void onlyOneAbstractMtd();
default void method1(){
System.out.println(“My Mtd 1”);
}
default void method2(){
System.out.println(“My Mtd 2”);
}
}
Method Reference in java → replacement of lambda expression, it is use to refer method of functional interface to an existing method.mostly use for code reuse ability. Functional Interface’s Abstract Method can be mapped to specific existing method using “::” operator.
Example: we have class Test which has static method testImpl
class Test{
public static void testImpl(){
System.out.println("Demo of method Referenceing");
}
,now i want to use my FunctionalInterface to call the testImpl mtd in MethodRefDemo class.
class MethodRefDemo{
public static void main(String arg[]){
MyFunctionalInterface fI =Test :: testImpl;
MyFunctionalInterface.onlyOneAbstractMtd();
}
}
In the above LOC the onlyOneAbstractMtd is referancing to testImpl this is how we implement Method Reference in java.
Also if no implementation is available in existing code then we use the below code,
class MethodRefDemo{
public static void main(String arg[]){
MyFunctionalInterface fI = () -> System.out.println("FunctionalInterface implementation");
fI.onlyOneAbstractMtd();
}
}
when we have existing implementation of Abstract method of our Functional Interface then we use method ref ,if no methods like testImpl is there use lambda expression.
Default methods → It is a way of adding the new methods to the interface without affecting the implementation class.
previously if we add a method in interface it gives compile time error in all classes which implements that interface ,so to avoid this default methods are added in java 8. This will also give Backward Compatibility if you use classes from older version jdk.
Ex: public interface MyDefaultInterface{
default void myPrint(){
System.out.println("Default Intefrace");
}
}public class UseDefaultInterface implements MyDefaultInterface{
public static void main(String arg[]){
UseDefaultInterface useme=new UseDefaultInterface();
useme.myPrint();
}
Can you override the default methods →Yes we can override, if required.
Is default keyword one of the access modifier → Default is not access modifier like public,protected or private, for default access we do not use any keyword. before java 1.8 it is use in switch case if nothing matches. It was never use in interface previously ,but now we use as default methods in interface to provide default implementation for all classes implementing it.
we cannot use default keyword for methods in class because default keyword is use in switch case.
How to override the default methods → yes, we can override default methods by keeping same method signature.
for example: we have interface MyDefaultInterface (above) now we want to override myPrint method in my implementation then
i can use same method signature however my access modifier will be public
public class OverrideDefaultMtd implements MyDefaultInterface{
public void myPrint(){ <-------note the access modifier default is public in class.
System.out.println("Override Default Method");
}
public static void main(String arg[]){
OverrideDefaultMtd o=new OverrideDefaultMtd();
o.myPrint();
}
Can you use hashcode() default implementation in interface — -> No, we are not allowed to override Object class method in Interface.All the implementing class by default has access to all methods of Object class.
How Default Methods in interface deals with Diamond problem → using interfacename.super.methodname().
(Diamond Problem → if two implemented interfaces contains same default methods then implementing class will get confuse as to which implementing method should it take )
ex: we have interface1 and interface2 with one same name method printing(), now we create a class diamond and implement both these interfaces, if we want printing method of interface1 then we can write interface1.super.printing(); similarly if we want printing() of interface2 we use interface2.super.printing();
why static methods were introduce in java 8 — -> static methods are introduce in interface because we can call those methods with just interface name, no need to create the class and then its object. interface do not have constructors,static blocks etc so consume less memory compare to class.
ex: public interface MyStaticInterface{
static void myStaticMtd(){
System.out.println("My Static Method");
}
}
public class Utility implements MyStaticInterface{
public static void main(String arg[]){
MyStaticInterface.myStaticMtd(); --> no object creation.
}
}
Are the Static Methods available to implementing classes by default → No, static method visibility is only to its interface,
it is not visible in the class which is implementing the interface. this is one drawback. example: in the above code for static interface we cannot call the myStaticMtd() creating the object.