2

I am new to Java. I want to know when should we use base class's reference and when we should create a new object. I mean we can write

List list = new ArrayList();

and when

ArrayList list = new ArrayList();

How to determine what to use ???

Richard H
  • 38,037
  • 37
  • 111
  • 138
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • Get http://www.wrox.com/WileyCDA/WroxTitle/Ivor-Horton-s-Beginning-Java-2-JDK-5-Edition.productCd-0764568744.html and a few bags of coffee (beans). – Augusto Feb 14 '11 at 11:18
  • 2
    possible duplicate of [List versus ArrayList](http://stackoverflow.com/questions/4062982/list-versus-arraylist) – Epaga Feb 14 '11 at 11:19

4 Answers4

7

It's ok to use both, but the former is preferred because it's always better to call an implementation's method using a reference to the Interface or super type (List). The former doesn't depend on the implementation and eliminates the need to change the code when the implementation (ArrayList) changes, but the latter requires you to change the code when the implementation changes to anything other than ArrayList.

asgs
  • 3,928
  • 6
  • 39
  • 54
4

use

List list = new ArrayList();

Always learn to program with interface. tomorrow you can have new implementation

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Can u explain why ? what flexibility i could get from List list = new ArrayList ??? – Maulik Feb 14 '11 at 11:20
  • That's assuming you only care that it's a List. What if you care that it's RandomAccess too? – jjujuma Feb 14 '11 at 11:21
  • @maulik, tomorrow suppose you want to use new LinkedList or any other implementation you just need to change the object only. I hope it does make sense – jmj Feb 14 '11 at 11:26
  • 2
    @maulik - note that this is even more relevant when you're declaring the types of method parameters. Most of the time with fields/local variables it's just a matter of changing two parts of a line instead of one, but specific method parameters can stop someone calling a useful method just because they happen to have a `LinkedList` instead of an `ArrayList`. Depending on what the method does, perhaps it should only insist on an `Iterable` (e.g. if it's just doing something to every element) so that even a `Set` or user-created collection could be used. – Andrzej Doyle Feb 14 '11 at 11:45
  • @Jigar can you reply on that http://stackoverflow.com/questions/11663351/establishing-the-urlconnection-to-another-web-application-does-not-work-with-url. Now i have mentioned error too and made the post precise. – M Sach Jul 26 '12 at 06:44
4

Addition to @Jigar my advise is to use that way for type safety

List< T > list = new ArrayList< T >();
2

Often it doesn't matter. An interface just defines a set of methods that you can call on an object: that is the object implements an interface. When it does matter is when you might have multiple implementations of an interface. Let's say you have a method that does something with an object that implements the Vehicle interface:

public static double getSpeed(Vehicle vehicle) {
    return vehicle.getSpeed();
}

You want to pass around the interface as you don't care whether the object is a car or a van or whatever, and you want this method to work for all implementations of this interface - ie all types of vehicle. They all implement the Vehicle interface so you know you can call the getSpeed() method. If you did this:

public static double getSpeed(Car car) {
    return car.getSpeed();
}

then your method wouldn't work for vans or lorries, even though they both implement the Vehicle interface and as such both have .getSpeed() methods.

Richard H
  • 38,037
  • 37
  • 111
  • 138