Thứ Ba, 10 tháng 2, 2015

changes in Java Interfaces

Interfaces:

    By using interfaces you can specify what a class must do,but not how it does.i'e it provides a contract to a class to implement defined methods.If you define one interface it can be used by any number of classes.Every field and method in an interface must be defined as a public,reason for this is class
must get access privilege.

    Prior to java8 interfaces does not allow any implementation of methods,but java8 allows default implementation for methods ,these methods named as default methods.Indirectly you can say java8 interfaces specify some behaviour like classes.Primary motivation for this feature  is expanding of an interface further with out breaking of an existing code.

Explanation in more detail:

    --If you want to provide any default behaviour to your interface create a default method .
   
    --Each default method will start with keyword "default".

    --This default method can directly accessed by implemented class.

Example:

    I will show you a simple example how we can use default method in an interface .lets observe below Interface


            public interface Java8Interface {
   
                       //abstract method must be implemented by its implemented class
                    public void sendMessage();
   
                        // a default method for this interface
                    default String currentMessage()
                    {
                            return "This is default Message";
                    }
           
                    }//end of interface
   
    --Above Java8Interface contains one default method and one abstract method.
   
    --implementaion for the sendMessage() will be provided by its contracted class,currentMessage() is a defaulte method you can observe its declaration preceded by default keyword.

Now lets define implemenatation class for Java8Interface



   

            //implement Java8Interface
            public class Java8Implementer implements Java8Interface{
   
                        //provide implementation for sendMessage()
                        public void sendMessage()
                        {
                            System.out.println("This message is not a default message");
                        }
   
   
                    public static void main(String args[])
                    {
                            Java8Implementer imp=new Java8Implementer();
                            //call sendMessage from here
                            imp.sendMessage()s
                            //now you can also call default method by using object imp
                            String msg=imp.currentMessage();
                            //print the message
                            System.out.println(msg);
                    }//end of main
   
                    }//end of class


let see the out put of Java8Implementer

output:
-------
This message is not a default message
This is default Message

i'e you can call a default method by using an object of a class by which an interface is implemented.

More About default implementation.
----------------------------------

    By above program we have a conclusion that a default method can be accessed by object of implemented class.However implemented class can also override existing interface default method.let see how it does

    Now am implementing same Java8Interface    with anothe class but this time am overriding currentMessage() of Java8Interface which is default






                    public class DefaultOverride implements Java8Interface{
   
                            public void sendMessage()
                            {
                                System.out.println("This is not a default message");
                            }
   
                            //now override default method
                            public String currentMessage()
                            {
                                   return"Message is overridden";
                            }
   
                            public static void main(String args[])
                            {
                                DefaultOverride def=new DefaultOverride();
                                    def.sendMessage();
                            //calling overrided method
                                String msg=def.currentMessage();
                                System.out.println(msg);
                            }//method
                    }//class

output
-------------
This is not a default message
Message is overridden

   

Không có nhận xét nào:

Đăng nhận xét