Interfaces are similar to classes, but the differences are they don't have instance variables and method definitions though methods are declared. In this tutorial, first, we will try to understand the need for interfaces and later we will write a sample code.
Case Study - Why interfaces are used?
Let us assume that you have an account with ABC Bank. You want to transfer an amount to your friend's account in XYZ Bank. Later you want to book a ticket from PQR. Hence, all the mentioned service providers can communicate with each other. We are assuming all the applications are developed using Java and they are communicating with each other using Remote Method Invocation.
How your account can be secured from unauthorized access?
Though this type of code is not written following such a simple technique. But to understand the problem scenario, we have taken this example.
As we have discussed earlier that instance variables are made private. Hence, generally, we define the scope of methods. So, if you transfer some amount to your friend's account, your friend's bank will call a method that is defined in your savings account class. Similarly for ticket booking, PQR will call a method that is defined in your savings account class. In both of the transactions, your savings account object reference will be sent to XYZ Bank or PQR.
In this context, XYZ Bank or PQR will be able to call the methods if they are made public. But different companies or organizations will have different types of legal agreements. So, if you define a set of methods as public, any one of them can access those methods. So from a technical perspective, it will not be secure enough. In this context, the solution can be provided by implementing different interfaces for different companies or customers.
By using access specifiers (like public, protected, default, or private) this kind of security can not be provided.
Here, we can define a savings account class containing all the required methods. We can also define two interfaces for XYZ and PQR. When an object will be requested by XYZ or PQR, the ABC server will send the object by interface reference (not the actual object reference). Hence, if XYZ bank wants to call any method of the savings account class, it can only call those methods which are declared in the respective interface. The same thing will happen for PQR.
Thus, even if many methods are defined in the savings account class, PQR or XYZ can call only those methods that are defined in the respective interface, which makes the application secure.
How to use Interfaces?
Let us design two interfaces Intfc1
and Intfc1
as shown below.
Intfc1.java
These two interfaces are implemented in class A
. This class contains more methods than the methods declared in the two interfaces.
A.java
If an object of the class A
is held by a reference of A
class, all the methods of A class are accessible by the reference. If the interface references are used to hold the objects, only those methods are accessible by the interface reference which are declared in that interface. The following example shows how to access objects by interface references.
TestMain.java
If a class implements one or more interfaces, but all the methods can not be defined in it. The class needs to be declared as an abstract class. And the undefined methods can be defined in subclasses.
Intfc1.java
Inftc2.java
A.java
B.java
TestMain.java
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.