1

I know that in Java most people declare a list using:

List l = new ArrayList();

But what would be the difference between that and

AbstractList l = new ArrayList();

What are the advantages of using an interface over an abstract class in this particular instance?

user1413969
  • 1,261
  • 2
  • 13
  • 25

3 Answers3

5

Original answer:

An abstract class allows you to define some shared functionality, while leaving other functionality to be defined by implementations. Thus, abstract classes are useful if you are going to create a family of similar classes that share some functionality but have customizations.

An interface doesn't allow you to define any functionality. It simply defines a set of method signatures that you know can be called on any object of a class that implements that interface.

I'd say the accepted practice is not to use an abstract class to fill the role of an interface. That is, an abstract class should be used for code sharing among related classes that you define, whereas an interface should be used for abstraction.

One reason not to use abstract classes to fill the role of interfaces is that a class cannot inherit from more than one class, but it can implement many interfaces. Thus, using abstract classes limits your design a lot more than using interfaces does.

Edit:

In this particular instance, the difference would be that if you later reassigned l to contain an object that implements List but does not derive from AbstractList, your code will throw an exception. Not all classes that implement List also extend AbstractList.

Using AbstractList limits you to only working with lists that are derived from the core AbstractList functionality. On the other hand, anyone could write a class that implements List using totally new code, and if you're using a variable of type List then you'll still be compatible with their new class that you've never seen before.

The fact that Java's lists are derived from AbstractList should be treated as an implementation detail that's internal to the Java library, not as an interface that you should code against.

Nate C-K
  • 5,744
  • 2
  • 29
  • 45
3

The hierarchy is like...

ArrayList extends AbstractList implements List

and

AbstractList implements List

So,

whenever you are creating a object of ArrayList like given below, You will be creating object of ArrayList with reference of List

List list = new ArrayList();

Another thing to note is, why it is required to implement List in both class, So,

it is for the purpose of showing that ArrayList implements List. AbstractList in the whole picture is just for convenience and to reduce code duplication between List implementations.

Reference: This SO Answer

Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80
  • The middle part of this appears to be incorrect. `AbstractList` implements `List`, so all methods of List can be accessed via `AbstractList`. – Nate C-K Jan 19 '14 at 19:07
3

In this case it wouldn't be really different. However it's a common code style to use the interface as the variable type, as the abstract implementation of AbstractWhatever class is mean to make it easier to create an implementation without having to implement all of the methods in the interface.

So it's a style issue, not really a technical one.

Kayaman
  • 72,141
  • 5
  • 83
  • 121