Realtime Use of Interface in Java Application

In the previous tutorial, we have known the basic of the interface in Java as well as its several advantages in utilizing the features of interfaces in general programming.

In general, when we write a simple program, nobody may think about the actual need of using an Interface.

But when we are developing a larger application, it is a good idea to use an interface. We can use it in most areas of application development.

The major advantage of using an interface in Java is that it allows multiple inheritance.

Let’s understand the use of interface in Java with realtime examples. We will understand how Java interface concept is useful in real-time application and software development.

Use of Interface in Java Application with Realtime Example

Realtime Example 1:

Suppose you have some rupees in your hands. You can buy from this money something from that shop where billing is done in rupees.

Suppose you go to such a shop for buying where only dollars are accepted, you cannot use your rupees there. This money is like a class. A class can fulfill only a particular requirement. It is not useful to handle different situations.

Now suppose you have a credit card. In a shop, you can easily pay in rupees by using your credit card. If you go to another shop where dollars are accepted, you can also pay in dollars.

The same credit card can be used to pay in pounds also. But how is this credit card paying in different currencies? Let’s understand it.

Basically, a credit card is like an interface that performs several tasks. It is a thin plastic card that contains identification information such as your name, bank name, and perhaps some numbers.

It does not hold any money physically. But here question is that how are shop keepers able to draw money from credit card?

Behind credit card, have our bank account details from where the money is transferred to shop keepers after authentication.

This bank account can be considered as an implementation class that actually performs different tasks. See the below figure to understand the concept.

Use of Interface in Java with realtime example

Real-time Example 2:

Suppose Amazon wants to integrate HDFC bank code into their shopping cart for their customer’s convenience. Amazon wants when their customers purchase any product from Amazon then they do payment through HDFC bank.

But to do payment Amazon does not have its own HDFC bank. So, they will have to take the help of the HDFC bank. Let’s say HDFC developed codes like below.

class Transaction 
 void withdrawAmt(int amtToWithdraw) 
   // logic of withdraw. 
   // HDFC DB connection and updating in their DB. 

Now, Amazon needs this class to integrate the payment option. So they will request HDFC bank to get this. But the problem with HDFC is that if they give the total code to Amazon then they are exposing everything of their database to them and logic will also get exposed which is a security violation.

Now what exactly Amazon needs, is method name and class name. But to compile their class they must get that class like below.

Transaction t = new Transaction(); 

If the first line has to compile at Amazon end, they must have this class which HDFC cannot give. The solution is here for HDFC that they develop an interface of Transaction class as given below.

Interface TransactionI 
  void withdrawAmt(int amtToWithdraw); 
class TransactionImpl implements TransactionI 
  void withdrawAmt(int amtToWithdraw) 
    // logic of withdraw. 
    // HDFC DB connection and updating in their DB. 

Amazon now will get an interface not class, they will use it as given below.

TransactionI ti = new TransactionImpl(); 
 // right hand side may be achieve by 
 // webservice or EJB 

In this case, both parties achieve their goals. Amazon only needs the method name and parameter they got it.

ICICI only want to give them name not logic so they provided. Also, it does not matter to ICICI what customers have purchased from Amazon. They just have to work for the amount deduction.

I hope that you will have understood from the above realtime example that how to interface is very useful in realtime applications. Let us look at a glance at some more uses of interface in java.

What is Use of Interface in Java?

There are mainly five reasons to use the interface in java. They are as follows:

1. In industry, architect-level people create interfaces, and then it is given to developers for writing classes by implementing interfaces provided.

2. Using interfaces is the best way to expose our project’s API to some other project. For example, HDFC bank can expose methods or interfaces to various shopping carts.

3. Programmers use interface to customize features of software differently for different objects.

4. It is used to achieve fully abstraction.

5. By using interface, we can achieve the functionality of multiple inheritance.

In this tutorial, you have learned about how to use an interface in realtime Java application or project. Hope that you will have understood the basic points of using interface in Java with realtime examples and enjoyed this tutorial.

In the next tutorial, we will learn nested interface in Java with the help of examples. If you like this tutorial, please share it on the social networking site for your friends.
Thanks for reading!!!

Next ⇒ Nested interface in Java⇐ PrevNext ⇒