Tuesday, December 30, 2014

Java interface

Interface is a powerful concept of java. Interface is playing a vital role in many real time projects. Interface is used to design a loose coupling software system which can be interfaced with any other software systems. As we had discussion already that, multiple inheritance is achieved by interface concept.
There are certain rules for an interface,

  1. Interface does not contains any method implementation
  2. All the methods in interface are public and abstract
  3. Any field declared in interface are by default public, static and final
  4. If an interface is implemented by a class, that class should implements all the methods which are declared an interface
These are the important rule with respect to interface in java

interface example:
     public interface Father {
           int age = 50;
           public void maintaineFamily();
           public void earnMoney();
     }

In above interface "Father" having a field called "age". "age" is a int variable and by default it is a "public static final int age = 50". Here we have a question, why it should be by default "public static final". We will discuss the reason later.
All the methods are in above interface is an abstract methods. In interface we no need to mention the abstract key for methods. By default all the methods are abstract methods.

Need of an interface

Multiple inheritance is not supported by java because of "Diamond problem". This problem can be overcome by interface.
An object may need IS-A relationship with many types,

For example,
      Modi is a PrimeMinister
      Modi is a Citizen
      Modi is a Politician

So, here if we create class "Modi", then "Modi" class needs the properties of "PrimeMinister", "Citizen" and "Politician". But unfortunately we can extends only one class in java. But there is no restriction if that an interface.

Here we have one more question, anyway interface does not have any implementation. All the implementations are given in class only. Then why interface is required? We will discuss for this question below with real time example

Real time example

There are many Databases in the market, each Database is working on different platform, different language and different environment. Java can't develop a code to connect with each Database, because java doesn't know how that Database is being designed and functioning. But java is having the capability to connect with all the Databases without separate implementation for each Database, how? that is because of interface.

Let we take it up Oracle Database for an example,

We are using below interfaces for Database related operations,

     Connection
     Statement
     ResultSet

For a Database connection,
The fisrt step is load the Driver class:
Driver class is nothing but a java class which is developed by the Database vendor with implementation of "java.sql.Driver" interface. So as per interface rule, that vendor driver class must implements all the methods of "java.sql.Driver" interface.

Next step is to get the reference of "Connection" interface:
To get the reference of "Connection" interface, we are calling "DriverManager.getConnection(DB_URL,USER,PASS)". If you look at the "getConnection" method, that is invoking the "connect" method which is declared in "java.sql Driver" interface and implemented in Database driver class. This "connect" method is returning the Database vendor's concrete class which implements the "Connection" interface. Now we got the reference of "Connection" interface.

Next step is to get the reference of "Statement" interface:
When we are calling the "createStatement" method with the reference of "Connection" interface, it will the returns the Database vendor's concrete class which implements the "Statement" interface.

Next step is to get the reference of "ResultSet" interface:
When we are calling the "executeQuery" method with the reference of "Statement" interface, it will the returns the Database vendor's concrete class which implements the "ResultSet" interface.

If you see all the above steps, all the methods of "Connection", "Statement" and "ResultSet" interfaces are implemented by Database vendor. So we no need to worry about Database design or language used in the Database. So java has defined the interfaces for the Database globally, all the Database should implements these interfaces to work with java.

We had a one question earlier of this session, that is why all the fields in interface must be public static and final.
From this above example, you can come to know that, interface is the mutual agreement between the two software system. So the values which are defined in the interface should not be modified during the run time by the either of the system. Thats why all the fields in interface are by default public, static and final.

This is how interface concept works with respect to Database connection.

No comments:

Post a Comment