Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is loose coupling achieved using interfaces in Java when an implementation class is mandatory and bound to interface contract?

How is loose coupling associated with interfaces when we are bound to create an implementation class regardless? The implementation class is forced to implement all those methods defined in the interface. I don't understand how this allows for lose coupling? I'm new to object oriented programming and software design so if you could shed some light on this topic it would super helpful. An example would totally be icing on the cake.

like image 887
Horse Voice Avatar asked Oct 20 '13 22:10

Horse Voice


People also ask

How can we achieve loose coupling in Java Using interfaces?

To achieve loose coupling, one should use abstract classes or interface while performing inheritance. The following examples will make the concept clearer. Output: Inside the foo method of derived class.

Why interface is loosely coupled in Java?

In a nutshell, loose coupling in Java is much better as compared to tight coupling. It provides better flexibility and reusability of code. As the two classes are independent of each other, it makes changes in the code very easy. It also provides better testability.

Can we achieve loose coupling by using abstract class and interface?

An interface can be used to achieve multiple inheritances and loose coupling, which we can't able to achieve by the abstract class.

How do you implement a loose coupling?

The general way to achieve loose coupling is through well defined interfaces. If the interaction between two systems is well defined and adhered to on both sides, then it becomes easier to modify one system while ensuring that the conventions are not broken.


3 Answers

The key point is that an interface doesn't just allow you to write one class which implements it, it allows you to write several.

When you have code which interacts with a class by using an interface, that code is able to work together with any class which implements said interface, regardless of how it implements it. That allows you to feed different classes to the same code without having to modify it.

Please note that interfaces are not the only way to reach a loose coupling of components. Loose coupling just means that components are able to work together without assuming anything about the internal workings of each other. You do that because the more your components treat each other as black boxes, the easier it becomes to do changes at one component without affecting any others. Interfaces can be one tool to work towards this goal, but neither are they required, nor are they the only tool which is worth mentioning in this regard.

like image 139
Philipp Avatar answered Oct 01 '22 05:10

Philipp


The implementing class is able to choose HOW to implement the functionality.

public interface PersonRepository {
    Person getPerson(String name);
}

Could be implemented by reading through a CSV file or by querying a database. The object which needs the person does not care how the person is found or loaded just that it is.

Hence it is deemed to be loosely coupled.

If it was tightly coupled it would need to know how to construct a SQL query or read a CSV file.

like image 35
Sam Turtel Barker Avatar answered Oct 01 '22 04:10

Sam Turtel Barker


the client code is coupled to the interface. it is not coupled to the implementation. you can change t he implementation without compiling the client code or the interface.

see http://en.wikipedia.org/wiki/Dependency_inversion_principle and http://en.wikipedia.org/wiki/Open/closed_principle

like image 27
Ray Tayek Avatar answered Oct 01 '22 06:10

Ray Tayek