Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Arrays in java violates ISP?

The code below will throw UnsupportedOperationException in java :

String[] arr = {"a", "b", "c"};
List<String> list = Arrays.asList(arr);
list.add("d");

I’m a little confused isn’t this an ISP violation ? Here class implements the List interface but can’t implement its method add()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, it does violate the Interface Segregation Principle, like any Unmodifiable collection. The designers of the Collections API could have created separate interfaces for write and read actions but decided to keep them together.

One could argue that this list does confirm to the ISP — it has the add() function — but the thing that it does with that function is not what you expect.

Here is a related discussion on Software Engineering Stack Exchange.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading