In this tutorial, we will learn about Interface Segregation Principle.It is one of the SOLID principles.
In simple term, Interface Segregation Principle dictates that client should not be forced to implement the methods which it won’t be able to use.You can always throw UnsupportedOperationException from the method which you don’t want to use but it is not recommended and it makes your class tough to use.
Let’s understand with the help of a simple example.
Assume we have created simple Set interface as below.
1 2 3 4 5 6 7 8 |
public interface Set<E> { boolean add(E e); boolean contains(Object o); E ceiling(E e); E floor(E e); } |
Create a class TreeSet.java as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; public class TreeSet implements Set{ @Override public boolean add(Object e) { // Implement this method return false; } @Override public boolean contains(Object o) { // Implement this method return false; } @Override public Object ceiling(Object e) { // Implement this method return null; } @Override public Object floor(Object e) { // Implement this method return null; } } |
Create another class HashSet.java as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; public class HashSet implements Set{ @Override public boolean add(Object e) { return false; } @Override public boolean contains(Object o) { return false; } @Override public Object ceiling(Object e) { // This method is not applicable for HashSet return null; } @Override public Object floor(Object e) { // This method is not applicable for HashSet return null; } } |
Do you see the problem, even though you do not require ceiling and floor method in HashSet, we have to implement them.
The correct solution for above problem will be:
Create another interface called NavigableSet which will have ceiling and floor method.
1 2 3 4 5 6 |
public interface NavigableSet<E> { E ceiling(E e); E floor(E e); } |
and Set interface will be changed as below
1 2 3 4 5 6 |
public interface Set<E> { boolean add(E e); boolean contains(Object o); } |
Now TreeSet.java will be going to implement two interfaces Set and NavigableSet. Change TreeSet.java as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.arpit.java2blog; public class TreeSet implements Set,NaviagableSet{ @Override public boolean add(Object e) { // Implement this method return false; } @Override public boolean contains(Object o) { // Implement this method return false; } @Override public Object ceiling(Object e) { // Implement this method return null; } @Override public Object floor(Object e) { // Implement this method return null; } } |
HashSet will be going to implement only Set as it does not require ceiling and floor methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class HashSet implements Set{ @Override public boolean add(Object e) { return false; } @Override public boolean contains(Object o) { return false; } } |
As you can see here, HashSet does not implement any method which it does not require.
That’s all about Interface Segregation Principle in java