• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections  RSS feed

 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why if i do

List list = new List();

the IDE tells me that i have to implement all abstract methods

and if i do

List list = new ArrayList();

i can use it without implemet anything ..
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course this is true, if you do new List() (List is an "interface", same as Map and Set too). If you do new ArrayList() (ArrayList extends List which already implements the List interface methods for you). Similarly like HashMap which extends Map interface or TreeSet which extends Set interface.

It is unusual to new List(), new Map() or new Set() directly.
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks is much more clear now
 
Tyagi.Kapil Tyagi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because List is an Interface and you cannot call the constructor of an interface. And ArrayList is a class which implements the List Interface and ofcourse you can call the ArrayList constructor. And according to the principle of 'run time polymorphism' you can write List l=new ArrayList(); ArrayList is a class that implements all the methods of List interface.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!