-4

Java Lists (also Collection) have method implementations (add(),get()) so how can they be INTERFACES? I would expect:

List <Integer> nums = ArrayList <Integer>();
nums.add(5);  

that the above code would be in error since add() should be implemented in ArrayList not List(being an interface) But that is not the case, the code works fine. Can someone explain this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Brett Penza
  • 39
  • 1
  • 4
  • 3
    "*Lists (also Collection) have method implementations (`add()`,`get()`)*" could you show them to us (BTW, declaration != implementation)? Also since Java 8 interfaces can have some [default methods](http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) but in `List` `add()` and `get()` is not one of them. – Pshemo Feb 01 '15 at 22:53
  • 4
    You seem confused as to what an interface actually is. – Dave Feb 01 '15 at 22:53
  • List has no implementations. Otherwise you could write List nums = List (); instead (which would not work). – DuKes0mE Feb 01 '15 at 22:59
  • 1
    @DuKes0mE Since Java 8 List has few implemented methods like [`sort(Comparator)`](http://docs.oracle.com/javase/8/docs/api/java/util/List.html#sort-java.util.Comparator-) so we no longer need to use `Collections.sort(list,Comparator)` but since it is still only interface it is possible that it can have other unimplemented methods, so we still can't initialize it via `new List`. – Pshemo Feb 01 '15 at 23:13

7 Answers7

3

This is just polymorphism in action. An ArrayList is-a List, and can therefore be assigned to a variable of type List.

The interface defines the contract of any class that implements it. It doesn't implement the methods (caveat: this is changing as of Java 8).

The variable nums is a List, but its concrete type is an ArrayList due to the assignment. The runtime treats it as polymorphic, and therefore the runtime dynamically binds to the add on the actual type that is stored in nums: i.e. it calls the add method on ArrayList.

I recommend reading The Java Tutorials > Polymorphism, and the pages on Interfaces in the same section.

Andy Brown
  • 18,961
  • 3
  • 52
  • 62
1

You seem to be somewhat confused as to what an interface is, or at least how you use them. interface in Java defines a set of method signatures that any class which implements the interface must provide (or else be abstract). ArrayList is one such class which implements the List interface. When you do the following:

List<Integer> nums = ArrayList<Integer> ()

You are creating an instance of ArrayList, which implements all the methods of List, and assigning it to a variable which can hold any object that implements List. This means that when using nums, you can only access methods exposed by List. However, the methods actually being called are those of ArrayList.

Java encourages using interfaces because they aren't bound to a particular inheritance chain. Consider the following method signatures:

public <T> void foo(List<T> input)
public <T> void bar(ArrayList<T> input)

The foo method can take anything that implements List; this could be one of the standard library objects, like ArrayList or LinkedList, or it could be a user-defined class that provides completely different functionality in addition to the standard List operations. This makes it much more flexible; foo is simply stating that expects an object that looks and acts like a List, but doesn't care what that object is.

The bar method, on the other hand, uses ArrayList directly. If later on you realize you want to use a different collection, you're stuck, because you can only use an ArrayList or a class which extends ArrayList.

aruisdante
  • 8,875
  • 2
  • 30
  • 37
1

An interface is a contract, and there is guaranteed to be some concrete implementation behind it. For example, you could use

List<Integer> nums = ArrayList<>(); 

or

List<Integer> nums = LinkedList<>(); 

They differ in terms of their internal implementation (but both ArrayList and LinkedList fulfil the contract specified by the List interface). Finally, in Java 8+ interface(s) can have default methods.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

implements List is a promise that the class has all the methods declared in the interface List. If all you need from an object is the List methods, it does not matter which class it actually is, as long as that class implements List.

List nums = new ArrayList (); declares nums to be a List reference. That means that any object it ever points to must be an instance of a class that implements List, and therefore has all the methods that List declares.

nums.add(5); uses the add method that any object nums references must supply, because it must implement List.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

As you can see here(OpenJDK source code): http://www.docjar.com/html/api/java/util/List.java.html

List is an interface with no implementation. You need to declare an ArrayList or another List which implements this interface

DuKes0mE
  • 1,101
  • 19
  • 30
0

You should study about inheritance. List is an interface that does not contain any implementation. But ArrayList contains all implementations of List interface.

When you call nums.add(5) you see List.add() method is being called but actually it calls ArrayList.add(5) method in background. That is the secret of Inheritance

Check this: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

emin
  • 742
  • 9
  • 21
0

Body of method is loaded dynamically (at runtime) based on actual type of object held by reference (it is called late or dynamic binding), so for code

List <Integer> nums = ArrayList <Integer>();

actual object in nums reference would be instance of ArrayList class, so code of add/get would be loaded from ArrayList.

List doesn't actually have implementation of get() method, but calling

nums.add(5);  

it is not a problem for compiler, because it knows that ArrayList will contain some implementation for all abstract (without body) methods from List interface.

Pshemo
  • 122,468
  • 25
  • 185
  • 269