Interfaces in Java – Explained in easy way!

In this post, I will talk about the real purpose of interfaces in Java.

When I was learning Java and Interfaces, all I know was that “An Interface does not have method behavior. You cannot create objects from an interface. Interface will extent another interface… blah blah”

Well, that’s what books will teach you. But, they don’t explain the real purpose of why Interface even came to existence. It’s only my experience in the industry that helped me uncover some of these mysteries..

Today, I will explain the real purpose of Interface that no book will teach you. I assure, after you read this article, you will have a clear picture on what are interfaces and their actual purpose. So, are you ready?

Let’s say that I want to write a code that will take a file path as an input and send that file to another computer/server. Don’t worry, I wont write annoying code. Just consider the case. So, Assume that there are two classes. ‘A’ and ‘B’. Class ‘A’ depends on Class ‘B’ and uses it’s methods to connect to server and send the file. So inside class ‘A’, you will have statements like these

B objB = new B();
objB.sendFileFromB(…);
objB.connecToServerFromB(…);

Like the statements above, I use the B class everywhere in my project in thousands of lines. A question for you.. Do you see any problem with the above statement? Well, I see one problem. Let’s talk about it..

Let’s say that I found out that B is not sending the file in secure manner, and hence I want to switch to a new class ‘C’ which provides the same functionality as of ‘B’. Unfortunately, it’s not so easy to start using ‘C’ class as my code is tightly coupled with ‘B’. I mean, I need to replace the above statements and match it with methods available in ‘C’ class. If i were to make those changes in entire project, it will take days of effort.

What is the solution? How can I write my code in such way, that if i decided to use another class/library, it shouldn’t take much of an effort to switch? Well, the answer is “Interface”. let me explain..

Assume we have an interface ‘I’ with the following methods signatures..

Interface I{

connectToServer();

sendFile();

}

Now, both the classes ‘B’ and ‘C’ implement that interface. By implementing, both ‘B’ and ‘C’ has to provide the behavior for these methods.

Inside my class ‘A’, I will have a statement like this..

I obj = new B();

obj.connectToServer();

obj.sendFile();

Now, notice few things. The reference type of above object is an interface but the instance was of ‘B’s. Meaning that by using ‘obj’, I can only access the methods that are declared inside the interface ‘I’, but the behavior would be provided by ‘B’.

in future, if I did not like ‘B’ and want to switch to ‘C’, then all I have to do is change the statement from this

I obj = new B();

to

I obj = new C();

That way, all the rest of the code will remain same. I don’t need to change any additional lines of code. This will certainly save me a great deal of effort required to refactor my code.

So now, what is an Interface?

And interface is kind of like a contract or a standard that classes would follow. In fact, if you think about why an Interface named as ‘Interface’, it is basically to say that it is used as an ‘Interface’ through which, you will call a method behavior.

That’s why, you should always code for an interface, not for a class. I.e.. always try to have interfaces as reference type, so that in an event that you need to use a different implementation, there is no need of code refactoring.

Leave a Reply