What is an interface
An interface is basically a contract saying "these are the methods that any class implementing this interface must implement".
Declaring a variable or field with an interface-type basically says "I don't care what the specific class is that gets assigned here, as longs as it follows the contract."
LinkedList
follows the contract set out by Queue
by implementing all the required methods.
Therefor we can assign a reference to an object of type LinkedList
to a variable of type Queue
.
"There can be no instance of an interface"
Developers often learn that "you can't instantiate an interface", which is correct but slightly misleading.
You can have an object that is an instance of an interface type by having an instance of a concrete class that implements that interface.
So while you can't execute new Queue<String>()
(since you can't instantiate an interface) the expression new LinkedList<String>()
actually returns a Queue<String>
(because a LinkedList<String>
is-a Queue<String>
, in a similar way that it is-a Object
).