Default method examples








Default  Method in Java8





Java 8   implemented functional interface.  It means java8 define a method in  interface.
 One method of interface   of  default type .  And define with default keyword. Java8 introduce
 Lamda expression  for  that we need  functional  interface.


    Example  functional   interface .


       Interface     Myinter{

                       Public   void     getNames();
                       default    void    nameList(){  
                              System.out.println(“This  is Employee Name list”);
                             }
                   }
  
      class     Employee   implements Myinter{
            public   void  getNames(){

                  //  code for  connection and  get data

                }

           Public   static  void main (String  args[]){
                             Myinter  my=new  Employee();
                                            my.nameList();
                    }      

         }

 We  have  defined  a  default  method  in   Myinter  interface .


Default Method and Multiple Inheritance Ambiguity Problems
Since Java classes can implement multiple interfaces and each interface can define a
 default method with the same method signature, the inherited methods can conflict with each 
other.
Consider the example below: 

public interface InterfaceA {
    default void defaultMethod(){
        System.out.println("Interface A default method");
    }
}
public interface InterfaceB {
    default void defaultMethod(){
        System.out.println("Interface B default method");
    }
}
public class Impl implements InterfaceA, InterfaceB  {
}
The above code will fail to compile with the following error:
java: class Impl inherits unrelated defaults for defaultMethod() from types InterfaceA and 
InterfaceB
In order to fix this class, we need to provide a default method implementation:
public class Impl implements InterfaceA, InterfaceB {
    public void defaultMethod(){
    }
}
Further, if we want to invoke default implementation provided by any super interface rather 
than our own implementation, we can do so as follows:
public class Impl implements InterfaceA, InterfaceB {
    public void defaultMethod(){
        // existing code here..
        InterfaceA.super.defaultMethod();
    }
}
We can choose any default implementation or both as part of our new method.
Difference Between Default Method and Regular Method
Default Method is different from the regular method in the sense that default method comes with default modifier. Additionally, methods in classes can use and modify method arguments as well as the fields of their class, but default method, on the other hand, can only access its arguments as interfaces do not have any state.
In summary, Default methods enable us to add new functionality to existing interfaces without breaking older implementation of these interfaces.
When we extend an interface that contains a default method, we can perform following,
  • Not override the default method and will inherit the default method.
  • Override the default method similar to other methods we override in subclass..
  • Redeclare default method as abstract, which force subclass to override it.









Default method examples Default method   examples Reviewed by Mukesh Jha on 12:52 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.