Use Interface in Java

A class that contains only abstract methods and no concrete methods becomes an interface.

Interface concept – Advantage:

A programmer wrote java program to connect to a database to for retrieving data from it, something like this.

The above class has a limitation. It can connect only to oracle database. If the client uses any other database then this code will not work. So we need to design the code in such a way that the code can be used to connect to any database. How it is possible ? Interface helps to solve this problem. Let us see how to write an interface in the following section.

Declaring an Interface

An interface is the specification of method prototypes. All the methods of the interface are  public  and abstract . When you are creating an interface, you are defining a contract what a class can do without saying anything about how class will do it. An interface is a contract.

Interface methods are  public  since they should be available to third party vendors or programmers to provide their implementation. They are abstract because their implementation is left for third party vendors or programmers.

Interface Implementation Classes

Now, let’s see how the clients (users) using different databases like  MySQL  and  PostgreSQL will provide implementation classes to  MyInterface  interface. For example MySQL people may provide implementation like the following for connecting to MySQL database and disconnecting from the database

Similarly, the PostgreSQL people may provide their implementation for connecting to PostgreSQL database and disconnecting from the database.

Note that implements keyword should be used with interface implementation classes

Why it is not possible to create object to an interface?

An  interface  contains only  abstract  methods which are all incomplete methods. So it is not possible to create object to an interface. But we can create separate classes where we can implement all the methods of interfaces. These classes are called implementation classes. And it is possible to create objects to the implementation classes.

Interface – Points to remember:
  • An interface is a specification of method prototypes i.e., only method names are written without method bodies.
  • An interface will have zero or more abstract methods. And all interface methods are implicitly  public  and abstract .
  • All variables defined in an interface must be public ,  static  and  final  i.e., interfaces can declare only constants, not instance variables.
  • Interface methods must not be  static .
  • You cannot create an object to an interface, but you can create a reference of interface type.
  • Since interface methods are abstract , they cannot be marked final .
  • An interface can extend one or more interfaces.
  • An interface cannot extend anything other than another interface.
  • An interface cannot implement another interface or class.
  • If any method is not implemented, then the implementation class should be declared as abstract .
  • Methods in the interface cannot be declared as private ,  protected  or static .
  • When an interface is written, any third party vendor can provide implementation classes to it.
  • Interface forces implementation classes to implement all of its methods compulsory.

Leave a Reply

avatar
  Subscribe  
Notify of