java.util.Queue<TreeNode> queue = new java.util.LinkedList<TreeNode>();
LinkedList implements Queue. Shouldn’t Queue be on the right side of the above statement and LinkedList the left?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Java, you can assign a value to a variable of the same type or a more general type. In your example,
new LinkedList<TreeNode>()is a value. SinceLinkedListimplementsQueue, it’s more specific thanQueue. i.e. ALinkedListis aQueue.For instance, all three of these are valid
But you can’t write any of these because they’re incorrect assignment.
P.S. the second one in that example is also invalid because Queue is an interface, and you can’t instantiate (
new) an interface because it’s not a concrete type