Friday, September 12, 2014

Interfaces

1.      Introduction
2.      Interface Methods
3.      Interface Variables
4.      Marker / Tag Interfaces
5.      Naming conflix in Interfaces

Introduction
An interface defines “Contract between client and service provider”.
From the client point of view an interface defines the set of service what is expecting
From the service provider point of view an interface defines the set of services what he is providing.

Advantages of Interface:
1.       As we are not highlighting our internal implementation. We can achieve security no one is allowed to see our internal logic or formula.
2.       Without effecting outside persons we are allowed to change our internal implementation. Hence enhancement is very easy.
3.       By Using interfaces two different system can able to communicate.
Declaring an Interface:
                  interface Test{
                        void m1(); //By default public abstract
                  }
The allowed modifiers for the interface are public, <default>, abstract, strictfp i.e. we are not allowed to declare interface as the final, violation leads to compile time error.
                  final interface Test{
                        void m1(); // By default public abstract
                  }
It will throw CE saying illegal combination of modifiers interface and final.

Every method present inside an interface is abstract. Hence an interface is considered as 100% pure abstract class.
                  interface Test{
                        void m1(); // By default public abstract
                        void m2();
                  }
                 
                  abstract class Sample implements Test{                     
                        public void m1(){}
                  }
                 
                  class Child extends Sample{
                        public void m2(){}
                  }
While implementing any interface method, compulsory it should be declared as public other wise compile time error.
If a class implementing any interface for all the interface method it should provide implementation otherwise the class has to declared as abstract.
A class can extend only one class at a time but an interface can extend any number of interfaces.
                  interface x{}
                  interface y{}
                  interface extends x,y{}

A class can implement any number of interfaces simultaneously. The following statements are not valid.
1.       A class can extend an interface.
2.       An interface can extend a class
3.       A class can implement another class
4.       An interface can implement another interface
5.       An interface can implement a class



Interface Methods:
Every interface method by default public and abstract always i.e. all the following methods decalaration inside an interface are equal
1.       void m1();
2.       public void m1();
3.       abstract void m1();
4.       public abstract void m1();  -> all are equal

Q: Which of the following method declarations are valid inside an interface.
1.       void m1() {} -> Wrong
2.       void m1(); -> Correct
3.       private void m1(); -> Wrong
4.       protected void m1(); -> Wrong
5.       synchronized void m1(); -> Wrong

Conclusion: As the interface methods are by default abstract and public, we are not allowed to declare interface methods with private, protected, static, native synchronized, final

Interface Variables
Every interface variable is by default public, static and final i.e. the following variable declaration are same inside an interface
1.       int i=10;
2.       public int i=10;
3.       public static int i=10;
4.       public static final int i=10; -> All are same.

As the interface variables are by default public, static and final we are not allowed to declare interface variables with the following modifiers private, protected, volatile and transient.
Eg:
                      interface Test{
                  int i=10;
            }
           
            class Sample implements Test{
                  public void m1(){
                        i=30;
                  }
            }
CompileTime exception cannot assign a value to the final variable.

As the interface variables are final in the implemented class we are allowed to access, but we cannot override it.

Method Naming conflicts in interface
Case 1: If two interfaces contain a method with same signature and return type then in the implemented class one method implementation is enough
            interface Left{
                  void m1();
            }
           
interface Right{
                  void m1();
            }

            class Test implements Left, Right{
                  void m1(){       
                  }
            } -> No exception, it works fine

Case 2: If two interfaces contain a method with the same name, but different arguments we have to provide implementation for the both the methods and these methods acts as overloaded methods
            interface Left{
                  void m1();
            }

            interface Right{
                  void m1(int i);
            }

            class Test implements Left, Right{
                  public void m1(){      
                  }
                 
                  public int m1(int i){
                        return i
                  }
            }

Case 3: If two interfaces having the same method name and same arguments, but different return type, It is not possible to implement those interface simultaneously.
            interface Left{
                  void m1();
            }

            interface Right{
                  int m1();
            }            

            class Test implements Left, Right{
                  public void m1(){      
                  }
                 
                  public int m1(){
                        return 10;
                  }
            }
It will throw compleTime exception.

Variable Naming conflicts in interfaces:
            interface Left{
                  int i =10;
            }

            interface Left{
                  int i =20;
            }

            class Test implements Left, Right{
                  public static void main (String args[]){
                        //System.out.println(i); -> CompliTimeException reference to i is ambiguous
                        System.out.println(Left.i); -> output is 10
                        System.out.println(Right.i); -> output is 20
                  }
            }

Marker / Tag interface:
 By implementing an interface if we are getting some ability such type of interface are called Marker or Tag interfaces.
Eg: Serializable, Clonable, Comparable, Runnable …. Etc.

These interfaces are made for some ability.
Note: If an interface doesn’t contain any method it is always marker interface. Even though an interface contains some methods if the objects will get some ability by implementing that interfaces are also considered as marker interfaces.

No comments:

Post a Comment