What Is the Benefit of Polymorphism Using Collection Interface to Create Arraylist Object

What is the benefit of polymorphism using Collection interface to create ArrayList object?

If you declare myList as ArrayList, you fix its concrete type. Everyone using it will depend on this concrete type, and it is easy to (even inadvertently) call methods which are specific to ArrayList. If sometime later you decide to change it to e.g. LinkedList or CopyOnWriteArrayList, you need to recompile - and possibly even change - client code. Programming for interfaces eliminates this risk.

Note that between Collection and ArrayList, there is another level of abstraction: the List interface. Typically the usage pattern of a list is very different from that of a map, set or queue. So the type of collection you need for a job is usually decided early on, and is not going to change. Declaring your variable as a List makes this decision clear, and gives its clients useful information regarding the contract this collection obeys. Collection OTOH is usually not very useful for more than iterating through its elements.

Polymorphism: Why use List list = new ArrayList instead of ArrayList list = new ArrayList?

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:

List list = new ArrayList();  

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,

List list = new LinkedList();

to

List list = new ArrayList();  

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.

Why should the interface for a Java class be preferred?

Using interfaces over concrete types is the key for good encapsulation and for loose coupling your code.

It's even a good idea to follow this practice when writing your own APIs. If you do, you'll find later that it's easier to add unit tests to your code (using Mocking techniques), and to change the underlying implementation if needed in the future.

Here's a good article on the subject.

Hope it helps!

Polymorphism and Java collections

The actual question seems to be:

You have

interface Fruit
class Apple implements Fruit
class Banana implements Fruit

Then you have a method

public String checkFruitColor(List<Fruit> fruit)

And you have

List<Apple> apples = //something
List<Banana> bananas = //something

Now, you have made the (common) assumption and List<Apple> is a subclass of List<Fruit> because Apple is a subclass of Fruit.

But, that means we could do

List<Apple> apples = new ArrayList<Apple>();
List<Fruit> fruits = apples //all good as List<Apple> is a subclass of List<Fruit>
fruits.add(new Banana()); //we can add a Banana to a List<Fruit>
final Apple apple = apples.get(0); //a List<Apple> will always have apples.

OOPS!!

So, in fact, a List<Apple> is not related to a List<Fruit> - as far as the compiler is concerned they are completely different. Put in technical terms, List is invariant in its type.

In order to make what you have work, you need to tell the compiler that you want a List of some subtype of Fruit but you don't care what.

public String checkFruitColor(List<? extends Fruit> fruit)

This will allow you to pass your List<Apple> to checkFruitColor and access the items as instances of Fruit.

What you cannot do is add() to the list as you do not know what the type of the List is.

ArrayList and List Interface Polymorphism?

Polymorphism work in the reverse order of what you understood, means that any object of a parent class/interface (in this case a List) can hold value of a child class (or a class that implements the interface):

List a = new ArrayList(); //correct
ArrayList b = new List(); //incorrect

so how can it return a List<> object?

You can use interface as a return type. Read this question for clarification.

Difference between Collection and Arraylist in Java?

The Collections API is a set of classes and interfaces that support operations on collections of objects.

Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

Whereas,
ArrayList: It is re-sizable array implementation. Belongs to 'List' group in collection. It permits all elements, including null. It is not thread -safe.

Collections: It implements Polymorphic algorithms which operate on collections.

Collection: It is the root interface in the collection hierarchy.

The following interfaces (collection types) extends the Collection interface:

  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Deque

Java does not come with a usable implementation of the Collection interface, so you will have to use one of the listed subtypes. The Collection interface just defines a set of methods (behaviour) that each of these Collection subtypes share. This makes it possible ignore what specific type of Collection you are using, and just treat it as a Collection. This is standard inheritance, so there is nothing magical about, but it can still be a nice feature from time to time.

Why should the interface for a Java class be preferred?

Using interfaces over concrete types is the key for good encapsulation and for loose coupling your code.

It's even a good idea to follow this practice when writing your own APIs. If you do, you'll find later that it's easier to add unit tests to your code (using Mocking techniques), and to change the underlying implementation if needed in the future.

Here's a good article on the subject.

Hope it helps!

Why to use Polymorphism?

let's look at OO design first, inheritance represents a IS-A relationship, generally we can say something like "let our FlyingMachines fly". every specific FlyingMachines (sub class) IS-A FlyingMachines (parent class), let say Jet, fits this "let our FlyingMachines fly", while we want this flying actually be the fly function of the specific one (sub class), that's polymorphism take over.

so we do things in abstract way, oriented interfaces and base class, do not actually depend on detail implementation, polymorphism will do the right thing!



Related Topics



Leave a reply



Submit