4

We have something like

List lst = new LinkedList();

which shows that List is some sort of Class. So, why call it an Interface? We can simply call it an Abstract class which implements Collection.

Nihal Sharma
  • 2,397
  • 11
  • 41
  • 57
  • 1
    I don't deserve a negative point by revealing the fact about the List being an Interface..... Moreover the question is not about when to use Abstract class or an Interface..its about whether list is an abstract class or an inteface – Kumar Vivek Mitra Aug 08 '12 at 07:36

3 Answers3

8

Interfaces and Abstract classes are used for different purposes. See this question.

A List defines a set of behaviour we want list-type objects to have, not the basis for a hierarchy of data structures. It doesn't need to specify any shared behaviour or anything like that. It just has the simple job of saying "everything that wants to call itself a List should be able to do these things"

Community
  • 1
  • 1
Oleksi
  • 12,947
  • 4
  • 56
  • 80
0

If a class is already extending another class It can not extend List in your case.

Basically you have to understand the difference between abstract class and interface.

or You may read Item 18 in Effective Java for better understanding.

Community
  • 1
  • 1
Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
0

List lst = new LinkedList();

Here List is A one type Collection with properties different from other collection types. LinkedList is implementation of these List properties using own data structure as LinkedList. ArrayList is another kind of data structure which implements same properties in its own way.

Collection is 100% Abstraction. Different type of collections have different properties. e.g List is A Collection with properties as Duplication is allowed. Order is important. etc Set is A Collection with properties as Duplication not allowed. Order may not be important etc

So since there is no common implementation logic required in different Collection types, there is no need to create Abstract Class.

Also List lst = new LinkedList(); provides some benefits of run time polymorphism. You can have some code which takes argument as List e.g. process(List lst). Here same code can be used by passing LinkedList or ArrayList etc. Let me don't go in details of that.

Go through difference between Interface and Abstract Class specified by Oleksi. Further I would like to simplify things. Main concept in OOPs is Abstraction which is generalized concept. Man is Animal. Animal is Living Thing. Now Living Thing is highest level of abstraction. Point to make here is, in case of Interface abstraction is 100%. In case of Abstract Class abstraction can vary between 0 to 100%.

Generally Abstract classes are used when some functionality needs to be provided by default and rest depends on class which is going to extend it. e.g. Suppose Animal is abstract class. Each animal has common thing say creatEnergy() which has common things as Food into Protein, Fat, etc which is common. So this implementation will be same for all animals. But other things like moving() may differ in case of Man, Bird, Fish etc Animals. Thus different animals may implement it differently. Thus here Abstraction vary from 0 to 100 %.

Again in Java Class can extend only one class but implement different interfaces. So interface provides a way for the class to have different behavior provided by interfaces at different time.

Hope this explanation clears your doubt. Let me know if need more explanation.

Free Coder
  • 459
  • 4
  • 19