A guide to Solid principles in java
Tags: java, design, programming, solid
Published: 2019-04-16 17:02:19

---
title: A guide to Solid principles in java
published: true
description: 
canonical_url: https://java2blog.com/solid-principles-java/
tags: #java #design #programming #solid
---
In this post, we will see 5 SOLID Principles in Java.


Robert C. Martin gave five objected oriented design principles, and the acronym "S.O.L.I.D" is used for it. Each letter of this acronym talks about principles in <a href="https://java2blog.com/core-java-tutorial-for-beginners-experienced/" target="_blank">Java</a>. When you use all the principles of S.O.L.I.D in a combined manner, it becomes easier for you to develop software that can be managed easily. The other features of using S.O.L.I.D are:
<ul>
 	<li>It avoids code smells</li>
 	<li>Quickly refractor code</li>
 	<li>Can do adaptive or agile software development</li>
</ul>
When you use the principle of S.O.L.I.D in your coding, you start writing the code that is both efficient and effective.
<h2><span style="color: #800000;"><strong>What is the meaning of S.O.L.I.D?</strong></span></h2>
As stated above, S.O.L.I.D represents five principles of Java which are:
<ol>
 	<li>S: Single responsibility principle</li>
 	<li>O: Open-closed principle</li>
 	<li>L: Liskov substitution principle</li>
 	<li>I: Interface segregation principle</li>
 	<li>D: Dependency inversion principle</li>
</ol>
The article discusses each of this principle in-depth. We will start by considering the very first principle, which is single responsibility principle.
<h2><span style="color: #800000;"><strong>Single Responsibility Principle (SRP)</strong></span></h2>
According to the <a href="https://java2blog.com/single-responsibility-principle-java/" target="_blank">single responsibility principle</a>, there should be only one reason due to which a class has to be changed. It means that a class should have one task to do. This principle is often termed as subjective.

The principle can be well understood with an example. Imagine there is a class which performs following operations.
<ul>
 	<li>connected to a database</li>
 	<li>read some data from database tables</li>
 	<li>finally, write it to a file.</li>
</ul>
Have you imagined the scenario? Here the class has multiple reasons to change, and few of them are the modification of file output, new data base adoption. When we are talking about single principle responsibility, we would say, there are too many reasons for the class to change; hence, it doesn't fit properly in the single responsibility principle.
<h2><span style="color: #800000;"><strong>Open Closed Principle </strong></span></h2>
According to <a href="https://java2blog.com/open-closed-principle-java/">open closed principle</a>, entities or objects should remain open for extension, but they should stay closed for modification. To be precise, according to this principle, a class should be written in such a manner that it performs its job flawlessly without the assumption that people in the future will simply come and change it. Hence, the class should remain closed for modification, but it should have the option to get extended. Ways of extending the class include:
<ul>
 	<li>Inheriting from the class</li>
 	<li>Overwriting the required behaviors from the class</li>
 	<li>Extending certain behaviors of the class</li>
</ul>
An excellent example of open closed principle can be understood with the help of browsers. Do you remember installing extensions in your chrome browser?

Basic function of chrome browser is to surf different sites. Do you want to check grammar when you are writing an email using chrome browser?If yes, you can simply use Grammarly extension, it provides you grammar check on the content.

This mechanism where you are adding things for increasing the functionality of the browser is an extension.  Hence, the browser is a perfect example of functionality that is open for extension but is closed for modification. In simple words, you can enhance the functionality by adding/installing plugins on your browser, but cannot build anything new.

Let's take another example.
You are using any Spring function functionality. You can obviously can not change core logic of it but you can extend Spring framework classes and create your own one.
<h2><span style="color: #800000;"><strong>Liskov Substitution Principle </strong></span></h2>
Liskov substitution principle assumes q(x) to be a property, provable about entities of x which belongs to type T. Now, according to this principle, the q (y) should be now provable for objects y that belongs to type S, and the S is actually a subtype of T. Are you now confused and don't know what Liskov substitution principle actually mean? The definition of it might be a bit complex, but in fact, it is quite easy. The only thing is that every subclass or derived class should be substitutable for their parent or base class.

You can say that it is a unique object-oriented principle. The principle can further be simplified by understanding this principle; a child type of a particular parent type without making any complication or blowing things up should have the ability to stand in for that parent.This principle is closely related to Liskov Substitution principle.
<h2><span style="color: #800000;"><strong>Interface Segregation Principle</strong></span></h2>
According to <a href="https://java2blog.com/interface-segregation-principle-java/">interface segregation principle</a>, a client, no matter what should never be forced to implement an interface that it does not use or the client should never be obliged to depend on any method, which is not used by them.

So basically, the interface segregation principles as you prefer the interfaces, which are small but client specific instead of monolithic and bigger interface.

In short, it would be bad for you to force the client to depend on a certain thing, which they don't need.

Let us now again take an example to understand this.

Let's take a simple example. You are implementing your own ArrayList and LinkedList in java. You create an interface called List which both classes will implement.
<pre class="highlight java"><code>package org.arpit.java2blog;

public interface List&lt;T&gt; {
	public T get();
	public void add(T t);
	public T poll();
	public T peek();	
}
</code></pre>
Let's create LinkedList class now.
<pre class="highlight java">package org.arpit.java2blog;

public class LinkedList implements List&lt;Integer&gt;{

	@Override
	public Integer get() {
		// Implement this method
		return null;
	}

	@Override
	public void add(Integer t) {
		// Implement this method
	}

	@Override
	public Integer poll() {
		// Implement this method
		return null;
	}

	@Override
	public Integer peek() {
		// Implement this method
		return null;
	}
}
</pre>
Let's create ArrayList class now.
<pre class="highlight java">package org.arpit.java2blog;

public class ArrayList implements List&lt;Integer&gt;{

	@Override
	public Integer get() {
		// Implement this method
		return null;
	}

	@Override
	public void add(Integer t) {
		// Implement this method
	}

	@Override
	public Integer poll() {
		// ArrayList does not require this method
		return null;
	}

	@Override
	public Integer peek() {
		// ArrayList does not require this method
		return null;
	}
}

</pre>
<strong>Do you see the problem</strong>, even though you do not require poll and peek method in ArrayList, we have implemented them.
<strong>The correct solution for above problem will be:</strong>
Create another interface called Deque which will have peek and poll method.
<pre class="highlight java">package org.arpit.java2blog;

public interface Deque&lt;T&gt; {
	public T poll();
	public T peek();	
}
</pre>
And remove peek and poll from list interface.
<pre class="highlight java">package org.arpit.java2blog;

public interface List&lt;T&gt; {
	public T get();
	public void add(T t);	
}
</pre>
Let's change LinkedList class now.
<pre class="highlight java">package org.arpit.java2blog;

public class LinkedList implements List&lt;Integer&gt;,Deque&lt;Integer&gt;{

	@Override
	public Integer get() {
		// Implement this method
		return null;
	}

	@Override
	public void add(Integer t) {
		// Implement this method
	}

	@Override
	public Integer poll() {
		// Implement this method
		return null;
	}

	@Override
	public Integer peek() {
		// Implement this method
		return null;
	}
}
</pre>
Let's change ArrayList class now.
<pre class="highlight java">package org.arpit.java2blog;

public class ArrayList implements List&lt;Integer&gt;{

	@Override
	public Integer get() {
		// Implement this method
		return null;
	}

	@Override
	public void add(Integer t) {
		// Implement this method
	}
}
</pre>
As you can see, we have seggregated two interface to achieve required functionality.
<h2><span style="color: #800000;"><strong>Dependency Inversion Principle</strong></span></h2>
According to this, dependency inversion principle, entities should depend only on abstractions but not on concretions. According to it, the high-level module must never rely on any low-level module but should depend on abstractions. Let us again understand it through another practical example.

You go to a local store to buy something, and you decide to pay for it by using your debit card. So, when you give your card to the clerk for making the payment, the clerk doesn't bother to check what kind of card you have given. Even if you have given a Visa card, he will not put out a Visa machine for swiping your card. The type of credit card or debit card that you have for paying does not even matter; they will simply swipe it. So, in this example, you can see that both you and the clerk are dependent on the credit card abstraction and you are not worried about the specifics of the card. This is what a dependency inversion principle is.
<h2><span style="color: #800000;"><strong>Wrap Up </strong></span></h2>
I hope that now you know the basic definition of all the five components of S.O.L.I.D, which are single responsibility principle, open, closed principle, Liskov substitution principle, interface segregation principle, and dependency inversion. So, basically, whenever you write code, you have to keep these core principle in your mind and actually, to be honest, you have to take these principles as your base map while writing those codes to make it efficient and effective.

The management of the code written by using these principles is also an easy task to do. Using this principle initially in your coding might look odd but you need to practice writing your codes using these principles, and gradually it will become a part of you. And then, the code that you have written can be easily modified, extended, refactored, or tested without facing any problem according to the requirement that you meet. So practice this principle whenever possible to make your coding better.
Source: <a href="https://java2blog.com/single-responsibility-principle-java/" target="_blank">Solid principles in java</a>

You may also like:
<a href="https://java2blog.com/xms-xmx-parameter-java/" target="_blank">xms and xmx parameter in java</a>
<a href="https://java2blog.com/java-reached-end-file-parsing/" target="_blank">Java reached end of file while parsing</a>
<a href="https://java2blog.com/java-lang-numberformatexception/" target="_blank">NumberFormatException in java</a>
<a href="https://java2blog.com/filenotfoundexception-java/" target="_blank">FileNotFoundException in java</a>
<a href="https://java2blog.com/java-isnumeric/" target="_blank">Check if number is numeric in java</a>