Recent Posts

Thursday, 7 July 2016

Interfaces Tutorial

* Any service requirement specification (SRS) is considered as Interface.

* From the client point of view an interface defines the set of services what is expecting.

* From the service provider point of view an interface defines the set of services what is offering.

* Hence an interface considered as contract between client and service provider.

E.g
     By using bank ATM GUI screen, bank people will highlight the set of services what they offering at the same time the same screen describes the set of services what end user is expected. Hence this GUI screen acts as contract between bank people and customers.

* With in the interface we can't write any implementation, because it has to highlight just the set of services what we are offering or what you are expecting. Hence every method present inside interface should be abstract. Due to this interface is considered as 100 % pure abstract class.

* The main advantage of interface are
1. We can achieve security because we are not highlighting our internal implementation.

2. Enhancement will become very easy, because without without effecting outside person we can change our internal implementation.

3. Two different systems can communicate via interface (A java application can talk with mainframe system through interface)

Declaration and implementation of an interface
* We can declare an interface by using interface keyword, we can implement and interface using implements keyword.
E.g
interface Interf {
   void m1(); // by default public abstract void m1();
   void m2();
}

abstract class ServiceProvider implements Interf {
   public void m1() {
       ---------
       ---------
   }
}
* If a class implements an interface compulsory we should provide implementation for every method of that interface otherwise we have to declare class as abstract. Violation leads to compile time error. 

* When ever we are implementing an interface method compulsory it should be declared as public otherwise we will get compile time error.

extends vs implements 
1. A class can extend only one class at a time.

2. A class can implement any number of interfaces at a time.

3. A class can extend a class and can implement any number of interfaces simultaneously.

4. An interface can extend any number of interfaces at a time.
E.g
interface A {
}

interface B {
}

interface extends A,B {
}
Interface methods
     Whether we are declaring or not, every interface method is by default public and abstract
E.g
interface Interf {
   void m1(); // public abstract void m1()
}
public: To make this methods availability for every implementation class.
abstract: Because interface methods specifies requirements but not implementation.
     Hence the following method declarations are equal inside interface
1. void m1()
2. public void m1()
3. abstract void m1()
4. public abstract void m1()
* As every interface method is bydefault public and abstract the following modifier are not applicable for interface methods.
1. private
2. protected
3. <default>
4. final
5. static
6. strictfp
7. synchronized
8. native
interface variables 
* An interface can contain variables the main purpose of these variables is to specify.

* Every interface variable is always public, static, final whether we are declaring or not. 
E.g
interface Interf {
   int x = 10;
}
public: To make this variable available for every implementation class

static: Without existing object also implementation class can access this variable

final:Implementation class can access this variable but cannot modify

     Hence inside interface the following declaration are valid and equal
int x = 10;
public int x = 10;
public static int x = 10;
public static final int x = 10;
public static int x = 10;
final int x = 10;
public final int x = 10;
static final int x = 10;
* As interface variables are public, static and final we cannot declare with following modifiers
1. private
2. protected
3. <default>
4. transient
5. volatile

* Inside implementation classes we can access interface variables but we can't modify their values
E.g
interface Interf {
   int x = 10;
}

class Test implements Interf {
   public static void main(String args[]) {
      x = 888; //C.E
      System.out.println(x);
   }
}
Interface naming conflicts

1. Method naming conflicts
Case 1
     If two interfaces contain a method with same signature and same return type in the implementation class we can provide implementation for only one method.
E.g
interface Left {
   public void m1();
}

interface Right {
   public void m1();
}

class Test implements Left, Right {
   public void m1() {
   }
}
Case 2
     If two interfaces contain a method with same signature but different arguments then in the implementation class we can provide implementation for both methods and these methods are considered as overloaded methods.
E.g
interface Left {
   public void m1();
}

interface Right {
   public void m1(int i);
}

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

   public void m1(int i) {
   }
}
Case 3
     If two interfaces contains a method with same signature but different return types. Then it is impossible to implement both interfaces at a time.
E.g
interface Left {
   public void m1();
}

interface Right {
   public int m1();
}
     We cannot write any java class which implements both interfaces simultaneously.

2 Variable naming conflicts
interface Left {
   int x = 888;
}

interface Right {
   int x = 999;
}

class Test implements Left, Right {
   public static void main(String args[]) {
      System.out.println(x);
   }
}
C.E: Reference to x is ambiguous
* There may be a chance of 2 interfaces contains available with same name and may rise variable naming conflicts but we can resolve these naming conflicts using interface names
System.out.println(Left.x);
System.out.println(Right.x);


Next Tutorial  Iinterfaces Tutorial Part-2

Previous Tutorial  Member Level Modifiers

No comments:

Post a Comment