Why should the interface for a Java class be prefe

2018-12-31 08:14发布

PMD would report a violation for:

ArrayList<Object> list = new ArrayList<Object>();

The violation was "Avoid using implementation types like 'ArrayList'; use the interface instead".

The following line would correct the violation:

List<Object> list = new ArrayList<Object>();

Why should the latter with List be used instead of ArrayList?

9条回答
孤独寂梦人
2楼-- · 2018-12-31 08:52

Spring framework applies the concept of interfaces really nicely - http://www.springframework.org/

Spring supplies the implementation to a concrete class via configuration file and so the concrete class need not know anything at all about the implementation.

Study of Spring exemplifies the advantages of Interface based programming in Java.

查看更多
有味是清欢
3楼-- · 2018-12-31 08:53

Using interfaces over concrete types is the key for good encapsulation and for loose coupling your code.

It's even a good idea to follow this practice when writing your own APIs. If you do, you'll find later that it's easier to add unit tests to your code (using Mocking techniques), and to change the underlying implementation if needed in the future.

Here's a good article on the subject.

Hope it helps!

查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 08:57

This is preferred because you decouple your code from the implementation of the list. Using the interface lets you easily change the implementation, ArrayList in this case, to another list implementation without changing any of the rest of the code as long as it only uses methods defined in List.

查看更多
一个人的天荒地老
5楼-- · 2018-12-31 09:00

Even for local variables, using the interface over the concrete class helps. You may end up calling a method that is outside the interface and then it is difficult to change the implementation of the List if necessary. Also, it is best to use the least specific class or interface in a declaration. If element order does not matter, use a Collection instead of a List. That gives your code the maximum flexibility.

查看更多
忆尘夕之涩
6楼-- · 2018-12-31 09:05

Why should the latter with List be used instead of ArrayList?

It's a good practice : Program to interface rather than implementation

By replacing ArrayList with List, you can change List implementation in future as below depending on your business use case.

List<Object> list = new  LinkedList<Object>(); 
/* Doubly-linked list implementation of the List and Deque interfaces. 
 Implements all optional list operations, and permits all elements (including null).*/

OR

List<Object> list = new  CopyOnWriteArrayList<Object>(); 
/* A thread-safe variant of ArrayList in which all mutative operations
 (add, set, and so on) are implemented by making a fresh copy of the underlying array.*/

OR

List<Object> list = new  Stack<Object>(); 

/* The Stack class represents a last-in-first-out (LIFO) stack of objects.*/

OR

some other List specific implementation.

List interface defines contract and specific implementation of List can be changed. In this way, interface and implementation are loosely coupled.

Related SE question:

What does it mean to "program to an interface"?

查看更多
临风纵饮
7楼-- · 2018-12-31 09:05

Properties of your classes/interfaces should be exposed through interfaces because it gives your classes a contract of behavior to use, regardless of the implementation.

However...

In local variable declarations, it makes little sense to do this:

public void someMethod() {
List theList = new ArrayList();
//do stuff with the list
}

If its a local variable, just use the type. It is still implicitly upcastable to its appropriate interface, and your methods should hopefully accept the interface types for its arguments, but for local variables, it makes total sense to use the implementation type as a container, just in case you do need the implementation-specific functionality.

查看更多
登录 后发表回答