when we compile the code. Compiler doesn't let you do the compile sucessfully because of this code
what happens exactly is , when we call poll method ,At compile time, compiler only checks the reference variable "l" type i.e., "List" contains poll method or not. if List contains that method then only compiler let you compile the code sucessfully....
I hope you understood...........
[ December 14, 2008: Message edited by: ShaikSalam Bashoeb ]
[ December 14, 2008: Message edited by: ShaikSalam Bashoeb ] [ December 14, 2008: Message edited by: ShaikSalam Bashoeb ]
ShaikSalem is correct, but maybe a little more detail would be welcome.
Go through the Java Tutorials, and look particularly at the List and Queue interfaces. If you look at the API you find that LinkedList is a kind of List, and it is also a kind of Queue (look at the top where it says about interfaces). If you say List<String> lll = new LinkedList<String>(); the compiler interprets that as:
Suresh wants a List.
At the moment he wants a LinkedList, but he might want to change to an ArrayList later. [You will never find a poll method in ArrayList.]
He said "poll" but I can't find poll in "List".
I had better not compile this bit.
If you click on the names of the interfaces (or find the full descriptions of the methods) in the API link, you can work out which interfaces the methods come from. If you said "List" then you are going to use the LinkedList as a List; if you said Queue then it would mean you are going to use the LinkedList as a Queue. In both instances you get the methods of those interfaces, and the other methods are not visible to the compiler.
If you had said Queue<String> qqq = new LinkedList<String>(); the compiler would have only found the methods which are in the Queue interface; you could then change the LinkedList to an ArrayDeque and it would still work.
Joined: Feb 22, 2007
Dear ShaikSalam Bashoeb, Campbell Ritchie
Thank you so much for your beautiful explanations. I really appreciate it.