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?

3
  • 1
    Possible duplicate of Java Polymorphism Commented Aug 7, 2017 at 18:26
  • Try a google search for "Java API". Read the pages for ArrayList, LinkedList, List, and Deque. The truth is out there. Commented Aug 7, 2017 at 18:27
  • You can read the java docs to see yes, LinkedList implements Deque. Also, read up about polymorphism and Object-Oriented Programming concepts. Commented Aug 7, 2017 at 18:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what would be the motivation for implementing the List in this case. Why not just have an Arraylist?
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.
@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.
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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.