2 added 132 characters in body
source | link

The two declarations can be different but are often the same. A common, recommended pattern in Java looks like:

List<String> list = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();

These variables list and map are declared using the interfaces List and Map while the code instantiates specific implementations. This way, the rest of the code only depends on the interfaces and it's easy to pick a different implementation classes to instantiate, like TreeMap, since the rest of the code can't depend on any part of the HashMap API that's outside the Map interface.

Another example where the two types differ is in a factory method that picks a specific subclass to instantiate, then returns it as the base classtype so the caller needn't be aware of the implementation details, eg a "policy" choice.

Type inference can fix the source code redundancy. Eg in Java

List<String> listOne = Collections.emptyList();

will construct the right kind of List thanks to type inference and the declaration

static <T> List<T> emptyList(); 

In some languages, type inference goes further, eg in C++

auto p = new Person();

The two declarations can be different but are often the same. A common, recommended pattern looks like:

List<String> list = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();

These variables list and map are declared using the interfaces List and Map while the code instantiates specific implementations. This way, the rest of the code only depends on the interfaces and it's easy to pick a different implementation classes to instantiate.

Another example where the two types differ is in a factory method that picks a specific subclass to instantiate, then returns it as the base class so the caller needn't be aware of the implementation details, eg a "policy" choice.

Type inference can fix the source code redundancy. Eg in Java

List<String> listOne = Collections.emptyList();

will construct the right kind of List thanks to type inference and the declaration

static <T> List<T> emptyList(); 

In some languages, type inference goes further, eg in C++

auto p = new Person();

The two declarations can be different but are often the same. A common, recommended pattern in Java looks like:

List<String> list = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();

These variables list and map are declared using the interfaces List and Map while the code instantiates specific implementations. This way, the rest of the code only depends on the interfaces and it's easy to pick a different implementation classes to instantiate, like TreeMap, since the rest of the code can't depend on any part of the HashMap API that's outside the Map interface.

Another example where the two types differ is in a factory method that picks a specific subclass to instantiate, then returns it as the base type so the caller needn't be aware of the implementation details, eg a "policy" choice.

Type inference can fix the source code redundancy. Eg in Java

List<String> listOne = Collections.emptyList();

will construct the right kind of List thanks to type inference and the declaration

static <T> List<T> emptyList(); 

In some languages, type inference goes further, eg in C++

auto p = new Person();
1
source | link

The two declarations can be different but are often the same. A common, recommended pattern looks like:

List<String> list = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();

These variables list and map are declared using the interfaces List and Map while the code instantiates specific implementations. This way, the rest of the code only depends on the interfaces and it's easy to pick a different implementation classes to instantiate.

Another example where the two types differ is in a factory method that picks a specific subclass to instantiate, then returns it as the base class so the caller needn't be aware of the implementation details, eg a "policy" choice.

Type inference can fix the source code redundancy. Eg in Java

List<String> listOne = Collections.emptyList();

will construct the right kind of List thanks to type inference and the declaration

static <T> List<T> emptyList(); 

In some languages, type inference goes further, eg in C++

auto p = new Person();