0

I am little bit confused with reference variable and object in Java.Please explain below line

    Queue<Node> q = new LinkedList<Node>();
LinkedList<Node> q = new LinkedList<Node>();

Now my question

  1. In first line, Why we are creating "q" variable of type Queue but and assigning object of type LinkedList?
  2. What we can and can't do with "q" if we will write above line as LinkedList<Node> q = new LinkedList<Node>();
  3. When we will require 1st line over 2nd line.Please give me some example.
dheerajraaj
  • 124
  • 13
  • It is not an issue of variable and Object, it is a matter of declaring an Interface rather than the actual Object. It adds more flexibility to the code. – Jeremy Grand Feb 21 '17 at 09:35
  • Its inheritance, try find shape triangle square inheritance example – Luminous_Dev Feb 21 '17 at 09:36
  • I'd advise you to at least read http://stackoverflow.com/questions/3383726/java-declaring-from-interface-type-instead-of-class , it answers your question. – Jeremy Grand Feb 21 '17 at 09:36

0 Answers0