Interface Segregation Principle in java

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.

public interface Set {
   boolean add(E e);
   boolean contains(Object o);
   E ceiling(E e);
   E floor(E e);
}

Create a class TreeSet.java as below.

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.

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.

public interface NavigableSet<E> {
   E ceiling(E e);
   E floor(E e);
}

and Set interface will be changed as below

public interface Set {
   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.

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.

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

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.