0

Suppose I want to create an ArrayList<> object.

Method 1:

ArrayList<Integer> myArrayList = new ArrayList<>();

Method 2:`

List<Integer> myArrayList = new ArrayList<>();

What is the difference and why do we have to use List interface as type? Explanation with examples would be appreciated. Thanks.

adhi
  • 29
  • 3
  • In that particular case `ArrayList` guaranties you can mutate it, `List` - doesn't. – Andrey B. Panfilov Aug 31 '22 at 06:02
  • 1
    My opinion: mostly because of a misunderstanding of where/when to use "program to interface" - there is no advantage for the code posted in the question, that is, for instances created locally and assigned to local variables - it should be used when declaring the parameters of a method (as shown in now deleted answer) or similar – user16320675 Aug 31 '22 at 06:23
  • @user16320675 I've undeleted my answer as maybe it is useful :). But answer is downvoted. – StepUp Aug 31 '22 at 06:38

1 Answers1

1

The List interface in Java provides a way to store the ordered collection

List interface is implemented by the classes of ArrayList.

Interfaces says that class must be able to do something. Interface is an abstraction. Interfaces are just contracts or signatures and they don't know anything about implementations. It can give you a chance to support many principles of OOP.

One of the design principle says:

program to interfaces, not implementations

Let me show an example via C#. I am sorry I am not Java guy. But I have not used any special feature of C#. I've added comments how it can be looked in Java.

We should create an interface:

interface IVehicle
{
    void Run();
}

and its concrete implementations:

public class Car : IVehicle  // Java: Car implements IVehicle 
{
    public void Run()
    {
        Console.WriteLine("I am Car");
    }
}


public class Plane : IVehicle  // Java: Plane implements IVehicle 
{
    public void Run()
    {
        Console.WriteLine("I am Plane");
    }
}


public class Sale : IVehicle  // Sale: Plane implements IVehicle 
{
    public void Run()
    {
        Console.WriteLine("I am Sale");
    }
}

Imagine that we have the following method:

void Run(Car car)
{
    car.Run();
}

The above method can run only concrete type such as Car.

Let's refactor the above method and apply design principle program to interfaces, not implementations. As a parameter, we will use an interface:

void Run(IVehicle vehicle)
{
    vehicle.Run();
}

As a result, now your method can run any vehicle, it doess not depend on concrete implementation of IVehicle such as Car, Plane, Sale.

And another reason to program interfaces is it is simpler to mock interfaces.

StepUp
  • 36,391
  • 15
  • 88
  • 148
  • 1
    probably downvoted because it is not Java but still useful IMHO (it is not that different from Java) - actually better than some other explanations about "program to interface" - it points the real advantage:/use when used for parameter types (not for local variables, as some even high upvoted answers state) – user16320675 Aug 31 '22 at 06:44
  • @user16320675 thanks for your comment. I've added a little bit comments how it can be looked in Java. Thanks! – StepUp Aug 31 '22 at 07:04