• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why super classes always declared as Interfaces and why not Abstract class?  RSS feed

 
ramsagar kyasan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While reading the design patter book, i got one doubt ,There is a List an interface having sub classes ArrayList, LinkedList etc.,
Q1) My question is Why they declared the List as interface rather than Abstract class?

Q2) i read some site -

List l = new ArrayList(); Why it is GOOD line?
ArrayList l = new ArrayList() ; Why it is BAD line?

Could you please answer with detailed information for Q1 and Q2.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all. Superclass can never be an interface as class is not an interface.
Supertype can be an interface.

A1. My guess is that interfaces ar emore flexible than abstract classes. A class can implement many interfaces but can extend only one class.
BTW, did you know that there is a class AbstractList?

A2. Generally because you should program to interfaces. Suppose you work on ArrayLists. Then you realize that LinkedList would be better in your program. So, you need to replace every single occurence of ArrayList to LinkedList.
In big programs this could be a major refactoring.
If, for example, you declared a type used to be List, you just need to change one place where a list object is actually created (from ArrayList to LinkedList).
BTW, BOTH of your examples are BAD because you are using raw collection types
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramasagar

Answer to both of your question is Polymorphism concept.

Thanks
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some more details :-
The main reason you'd do this is to decouple your code from a specific implementation of the interface. When you write your code like this:

the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.
For instance, say you were writing a fairly large 3rd party library, and say that you decided to implement the core of your library with a LinkedList. If your library relies heavily on accessing elements in these lists, then eventually you'll find that you've made a poor design decision; you'll realize that you should have used an ArrayList (which gives O(1) access time) instead of a LinkedList (which gives O(n) access time). Assuming you have been programming to an interface, making such a change is easy. You would simply change the instance of List from,

to

and you know that this will work because you have written your code to follow the contract provided by the List interface.
On the other hand, if you had implemented the core of your library using LinkedList list = new LinkedList(), making such a change wouldn't be as easy, as there is no guarantee that the rest of your code doesn't make use of methods specific to the LinkedList class.
All in all, the choice is simply a matter of design... but this kind of design is very important (especially when working on large projects), as it will allow you to make implementation-specific changes later without breaking existing code.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramsagar kyasan wrote:Q1) My question is Why they declared the List as interface rather than Abstract class?

And my question to you is: Why do you think it might be a good idea?

The designers of Java weren't stupid, so presumably they must have had some reason for creating a List interface, rather than a superclass for all lists.

The first answer you will usually hear is: "Single inheritance" - a Java class can only inherit from one superclass, whereas it can implement any number of interfaces - and IMO it's wrong.

The decision was made for you, the programmer, not because of single inheritance; and a List interface is as defendable in a language (like C++) that offers multiple inheritance as one that doesn't.

The reason is that a "list" has certain properties - specifically, that it's an ordered collection of items - and therefore has a predictable behaviour which can be defined by methods, no matter how it's implemented.

A "bag", on the other hand, is a completely different type of collection - even though it might share some of the same characteristics (eg, size). If you toss something into a bag that already contains 40 items, would you expect someone else to be able to simply reach in and take the same item out? But if you asked a kid to add a letter at the end of a line of letter blocks, and then another one to remove the last one, it would be quite simple, no?

That's the difference between a "Bag" and a "List", and why should I care how someone wrote it, as long as they did it correctly?

And that's the essential difference between an interface and an abstract class. An interface simply defines WHAT is required; an abstract class may (and often does) also implement HOW it's done.

HIH. And for further info, you might want to read the WhatNotHow (←click) page.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!