0

So I have a line of code that goes:

List<Deque<Integer>> pegs = new Arraylist<>();
for(int i=0; i< NUM_PEGS; i++){
   pegs.add(new LinkedList<Integer>());
}

I am wondering what extra functionalities does an Arraylist get by implementing the interface List? And is the reason I am able to add a LinkedList to pegs because a LinkedList implements the Deque interface?

John
  • 35
  • 6
  • 1
    Possible duplicate of [Java Polymorphism](https://stackoverflow.com/questions/10985223/java-polymorphism) – Tezra Aug 07 '17 at 18:26
  • Try a google search for "Java API". Read the pages for ArrayList, LinkedList, List, and Deque. The truth is out there. – DwB Aug 07 '17 at 18:27
  • You can read the java [docs](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) to see yes, LinkedList implements Deque. Also, read up about polymorphism and Object-Oriented Programming concepts. – Tezra Aug 07 '17 at 18:27

2 Answers2

5

I wouldn't say that an ArrayList gets extra functionality by implementing the List interface. It actually provides more functionality over what is specified in the List contract. You can compare the documentation for List and ArrayList to see the differences.

For your second question, yes, you can see in the documentation for Deque that it is implemented by LinkedList, among others.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • what would be the motivation for implementing the List in this case. Why not just have an Arraylist? – John Aug 07 '17 at 18:33
  • 1
    Exactly. Interfaces are like types that specify the minimum methods a class must have, they don't add functionality, they guarantee minimum functionality. One can reach this functionality through the interface type, but the class still has to have it defined in the base class. – Edwin Buck Aug 07 '17 at 18:33
  • @John The point is that you can write methods that accept *at least* a List (anything that implements List), without caring if a LinkedList, ArrayList, or any other kind of List is passed in. You know they implement at least the functionality of the List interface. – Bill the Lizard Aug 07 '17 at 18:35
3

Normally, a class does not receive any "extra functionalities" by implementing an interface.

(This has changed a bit since java 8, because interfaces can now contain default methods, so it is actually possible to receive extra functionality by implementing an interface, but that's not what this question is about.)

When implementing an interface, a class has to provide all the functionality that the interface requires to be provided. Code must be added to the class to provide that functionality. ArrayList does all that in order to offer the List interface.

So, you might ask, what's the point of implementing an interface?

The point is that then in your code you can refer to the list as simply a List, without knowing that it is actually an ArrayList, which means that your code can then work with any class that implements the List interface, not just with ArrayList.

As for your 2nd question: the answer is "yes".

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142