Interfaces In Java


Interfaces in Java

In the previous post, we have discussed more on abstract class and code re-usability aspect. Now let us discuss on another important artifact of java “interfaces”. Abstract class and interfaces may look(but it is not and never) like same with few differences. But the actual use of interface is totally different from abstract class. An interface is a guarantee or contract between parties to work together. When a class provides (in Java, it’s called “implements”) an interface, it is guaranteeing that it will have public implementations for the methods listed in the interface.

Abstract class or parent class would fit only if it satisfies “Is-a” relationship. But interfaces designed to add special or additional functionality to the class. By implementing the interface, it guarantees that the implemented class provides the functionality expected by the interface. Third party users confirms that the implemented class would provide implementation of all the methods. So interface to make broadly known the guaranteed behavior of the class. Let us see with an example,

interface Drawable
{
void draw(int color);
}

class Circle extends Shape implements Drawable
{
private double x, y, radius;
//………………………..
@Override
public void draw(int color)
{
System.out.println(“Circle drawn at (” + x + “, ” + y +
“), with radius ” + radius + “, and color ” + color);
}
}

class BluePrint implements Drawable
{
//………………………..
@Override
public void draw(int color)
{
System.out.println(“Blue print of the building with width and height.”);
}

}

class Tower extends Building implements Drawable
{
//………………………..
@Override
public void draw(int color)
{
System.out.println(” Draw tower structure and building.”);
}

}

Here we can see that classes Tower,BluePrint and Circle are not releated to each other. But all implements the interface Drawable to add special functionality “Draw” to all the classes. So the third party user can confirm that if class implements the “Drawable” would provide “Draw” method as a guarantee. Java’s standard class library interface end with the “able” suffix. Examples include Callable, Cloneable, Comparable, Formattable, Iterable, Runnable, Serializable, and Transferable. Try to maintain same kind of naming convention for your interface as well.

A class can implement mulitiple interface. Like making a class more powerful by adding more functionality.
Never use interface for “in-a” relationship.
Abstract class and subclass relationship makes sense only if each classes related together. But interfaces can be used in all required circumstances.

Next use of interface is to make a contract between two parties in API programming. For instance a middle-ware to interact with multiple external system, it required to invoke external vendor methods. If each vendor creates different methods with different names, it is hard to work with external system as Middle ware is not clear about method names and its internal implementation. To rule out all these issues, Middle-ware creates list of interfaces and document the expected behavior of each method. Then the external vendor get the interface definition and implementation all the method of the interfaces as documented by the middle ware. Here interface acts as contract between middle ware and external systems.

Classic example for interface contract model is JDBC connectivity. Java(consider as middle-ware) to support all the external system(Oracle, MySQL, SQL server and other databases. It publishes the jdbc interfaces java.sql.Connection, java.sql.Statment and java.sql.ResultSet. The external vendors Oracle,MySQL and other databases organization implements these interfaces and publish the implementation(ojdbc14.jar ,mysql-connector-XXX.jar) work with Java ecosystem. Java organization validates the third party implementation of all the interfaces and methods. Once it confirms the contract as documented, it announced as supported external system.

In interface, Fields that are declared are implicitly public final static and method are implicitly public abstract.

In case we want to declare static variables to use in multiple module, use interfaces for constants. Never ever create class only for constants. For example,

interface BusinessConstants{

public static final float PI = 3.14f;
public static final int RETRYCOUNT = 10;
public static final String ORG_NAME = “javadeveloperkit.com”;

}

Here we declared PI, ORG_NAME and RETRYCOUNT are final constants and it can be access by interface name. Here we discussed when to use interface in the design. Let us share more on interface in comments section.

Key-points to remember:

Don’t use interface for “Is-A” relationship.
An interface is a guarantee or contract between parties to work together.
A class can implement mulitiple interface. Like making a class more powerful by adding more functionality.
Fields that are declared are implicitly public final static and method are implicitly public abstract.
Use interface to declare static final constants.

Tags: ,

You may also like