Use of Java Interface In Realtime Application - Scientech Easy

Use of Java Interface In Realtime Application

In the previous tutorial, we have known basics 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 Interface. It can be used in most area of application development. The main advantage of using interface in Java is that it allows multiple inheritance. Let's understand with examples of how Java interface concept is useful in real-time application and software development.

Real-time 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 which 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 which actually performs different tasks. See the below figure to understand the concept.
Use of Java interface

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 their own HDFC bank. So, they will have to take the help of 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 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(); t.withdrawAmt(500);
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 ti.withdrawAmt(500);
In this case, both parties achieve their goals. Amazon which only needs 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 customer have purchased from Amazon. They just have to work for the amount deduction.

I hope that you will have understood from above example that how to interface is very useful in real-time application.

Final words 
Hope that this tutorial will have helped you to understand the use of Java interface in real-time application development. I hope that you will have understood and enjoyed this tutorial. In next tutorial, we will learn nested interface in Java.
Thanks for reading!