-1
List<String> list = new ArrayList<String>();

What is List and ArrayList when identifying the parts besides super class and subclass?

Is List the reference and ArrayList the class?

Would they be called something else if they were the same like:

ArrayList<String> list = new ArrayList<String>();
Cœur
  • 37,241
  • 25
  • 195
  • 267
tazboy
  • 1,685
  • 5
  • 23
  • 39
  • The super class does not need to be an interface, it could be another class. The duplicate link you provided takes about interfaces. – tazboy Feb 01 '16 at 16:27
  • Conceptually, it is the same. `List` is a super-type of `ArrayList`. And `List`is an interface that is implemented by `ArrayList` (and there are a bunch of implementations). With such a construct, you are effectively doing what is called “programing to an interface”. Hence the linked question. – Tunaki Feb 01 '16 at 16:33

4 Answers4

1

List is an interface. ArrayList is an implementation of it. Neither is a superclass or subclass of the other.

jsheeran
  • 2,912
  • 2
  • 17
  • 32
1

There are three parts to this declaration:

  1. A named reference (list)
  2. The compile time, static type of the reference (List<String>)
  3. The run time, dynamic type of the reference (ArrayList<String>)

Each of those pieces would retain their character regardless of how you modified the compile time type.

List is an interface; ArrayList is an implementation of that interface.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

In the first example List is the interface which defines the type of reference to the object list and ArrayList is the implementation of the interface which is the type of the actual object. In the second example, both object and its reference have type ArrayList

Pooja Arora
  • 574
  • 7
  • 19
0

Difference:

  • First one, Only methods of the interface List can be invoked on the list object. polymorphisme is present here.
  • Second one, methods of ArrayList class can be invoked on the list object.

Advantages of the first one:

Even if you change the implementation to:

List<String> list = new LinkedList<String>();

or

List<String> list = new Vector<String>();

You have not to change the rest your code.

Mohamed Bathaoui
  • 340
  • 1
  • 14