I was reading http://www.docjar.com/html/api/java/util/LinkedList.java.html
When you declare a queue in Java
Queue<Integer> queue = new LinkedList<Integer>();
What happens behind the scene ? because I see queue is an interface with just method signatures, and LinkedList doesn’t directly implement it, so how does it override those methods (add(), peek(), poll(), offer(), and remove() ) and do the polymorphism like that ? I mean you can only access some certain methods but not all of them from LinkedList for example public void add(int index, E element) is no longer available as it makes the apparent type to Queue. Also didn’t we need to cast it ?
From the source code of the JDK:
So
LinkedList<E>doesn’t directly implementQueue<E>, but it does implementDeque<E>, which extendsQueue<E>:Threfore,
LinkedList<E>inherits the abstract methods ofQueue<E>.The overriding methods are defined directly in
LinkedList<E>– as usual.