Introduction

Interfaces
  • 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.
Advantages

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.
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.
interface A {
}

interface B {
}

interface extends A,B {
}
Introduction


Scroll to top