Thursday, February 10, 2022

5 Benefits of using interface in Java and Object Oriented Programming

Interface in Java is a simple concept but many programmers fail to realize their actual use, including me. When I started learning Java programming, I learned interface is something where you can declare functions but cannot define them. To me, they were useless at that time because there was no code to do anything. It took me years to realize how useful an interface can be. They are not intended to do things but they are actually facilitators. They provide abstraction and give the flexibility to change your program in the future. That's why it's very important for a Java programmer to understand the benefits of interfaces and realize how and when to use them in code. 

Since many of my readers ask me questions about the actual use of the interface in java and the benefits it provided, I have decided to jot down these benefits and explain them to you in simple words. 

These are the benefits I have realized from my own experience so it's both tried and tested and battle-hardened. If you have been coding in Java then there is a high chance that you are already familiar with them and may even be using them in your code but if you are, this is the right time to learn, understand and use them in your code. 




5 Advantages of using an interface in Java

Here is a list of the main advantages of using an interface in Java. THese are also the most important reasons I use interface while coding in Java. 


1. Easier to Test
One of the key benefits of using an interface in Java is Testing, the interface makes unit testing easier. The best example of this is DAO pattern and parser classes. If your code is using interface for dependencies then you can easily replace them with a mock, stub, or a dummy for testing. 

For example, if your code needs a Cache and you are using an interface variable to hold that dependency, you can write a HashMap based Cache implementation for testing like below:

class BookRepository{
private interface Cache bookCache;

public boolean isExists(Book aBook){
  return bookCache.contains();
 }

}


In this code, if you want to test isExists() method, you need to create an object of BookRepository which needs a Cache. If this class loads data from a database, and build cache then it would be difficult to test because loading data from the database takes time. 

If you use the interface, you can swap the actual cache with a dummy in-memory hashmap based cache and test with lightning speed.

 

4. Faster Development
This is the fourth practical benefit of interface I have noticed while working on a Java project. In a real project, you often have dependencies on your partner's and colleagues' work. 

For example, if your application is a client-server application where the client sends and receives data from the server then you may need to wait until your partner finishes server-side work before you can start client-side work.

If you use an interface, you can work around this becuase the interface doesn't require any actual implementation. All you need to agree on is the name of the interface and operation on them which is much easier than actually coding those operations. To be honest, this has saved a ton of time in our case. 

2. Increased Flexibility
The third benefit of the interface in Java is that it increases flexibility or result in more flexible code. You may be wondering what is flexibility? well, it means your code should be flexible to change. It should be easy to use different logic at different times. 

For example, a TracelCostCalculator should be able to calculate fair for bus, train, or plane journey. This is achieved by using different implementations of the same interface. 

Anyway, this example may not be the best to prove the point but it does make intention clear. If you have a better example, feel free to share it with us. 



4. Loosely couple code
The first benefit of the interface in Java is that it creates loosely coupled code, which is easier to change and maintain. In a loosely coupled code, you can replace pieces without affecting the whole structure.

While in a tightly coupled code, it's not possible to replace one part of code without impacting other parts of code. I am not able to think of an appropriate example now, but I'll add it later. In the meantime, if you have any, just drop a note and we can discuss. 



5. Dependency Injection
Finally, the Dependency injection is based on an interface. In the case of DI, instead of a developer, the framework injects dependencies and this is achieved by declaring dependencies as interfaces. 

Spring framework is a good example of how the interface can help with dependency injection. Many key parts like Controller, Model, and View are interfaces on Spring MVC

5 Benefits of using interface in Java

And last, Just remember coding for interface pattern, which I have discussed on 10 OOP design principles every programmer should follow for writing clean code. 


That's all about some of the main benefits of using an interface in Java. These are some hard-earned lessons I have learned while using the interface in actual code. I have to admit that I am a bit too lazy while writing this piece and could have done a better job with examples but then this post would have never gone live. Well, it's actually sitting on my notes from the last 3 years :-) 

So, I just thought to push it and improve it later. Sometimes, comments and questions from reader motivate you to add more content and examples. And, if you have better examples, feel free to drop a note, we can discuss and I can also include them in the main article. 


Other Java and Programming Resources you may like
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

No comments:

Post a Comment