Interfaces in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts


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
public interface Intfc1 { public void method1(); public void method2(); }


public interface Intfc2 { public void method1(); public void method3(); public void method4(); }

These two interfaces are implemented in class A. This class contains more methods than the methods declared in the two interfaces.

A.java
public class A implements Intfc1, Intfc2{ public void method1() { System.out.println("I am in method1."); } public void method2() { System.out.println("I am in method2."); } public void method3() { System.out.println("I am in method3."); } public void method4() { System.out.println("I am in method4."); } public void method5() { System.out.println("I am in method5."); } }

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
public class TestMain { public static void main(String[] args) { A a = new A(); a.method1(); a.method2(); a.method3(); a.method4(); a.method5(); Intfc1 i1 = new A(); i1.method1(); i1.method2(); Intfc2 i2 = new A(); i2.method1(); i2.method3(); i2.method4(); } }

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
public interface Intfc1 { public void method1(); public void method2(); }
Inftc2.java
public interface Intfc2 { public void method1(); public void method3(); public void method4(); }
A.java
public abstract class A implements Intfc1, Intfc2{ public void method1() { System.out.println("I am in method1."); } public void method4() { System.out.println("I am in method4."); } public void method5() { System.out.println("I am in method5."); } }
B.java
public class B extends A { public void method2() { System.out.println("I am in method2."); } public void method3() { System.out.println("I am in method3."); } }
TestMain.java
public class TestMain { public static void main(String[] args) { B b = new B(); b.method1(); b.method2(); b.method3(); b.method4(); b.method5(); Intfc1 i1 = new B(); i1.method1(); i1.method2(); Intfc2 i2 = new B(); i2.method1(); i2.method3(); i2.method4(); } }


Happy Exploring!

No comments:

Post a Comment