Someone who is just starting with Java programming language often has doubts about how we are storing an ArrayList object in List variable, what is the difference between List and ArrayList? Or why not just save the ArrayList object in the ArrayList variable just like we do for String, int, and other data types. Well, the main difference between List and ArrayList is that List is an interface while ArrayList is a class. Most importantly, it implements the List interface, which also means that ArrayList is a subtype of the List interface. In Java or any object-oriented language, the supertype of a variable can store an object of subtype.
This is known as Polymorphism because any virtual method will be executed from subclass only, even though they were called from the supertype. This is the beginning, now let's see those two questions as well.
Anyway, If you are not familiar with Inheritance and other object-oriented programming concepts in Java, I suggest you first go through a comprehensive course like The Complete Java Masterclass, which is the most up-to-date online course to learn Java. Always updated for the latest Java version like Java 12.
Here we are using a List as a type of variable to store an object of ArrayList class, created using the new() operator. This is known as programming for the interfaces. In fact, everywhere you need to declare a reference variable, you should always use the supertype, like Map for passing HashMap, Set for giving HashSet, and List for passing ArrayList, LinkedList, or Vector.
You should use interface as type on the return type of method, type of arguments, etc. as shown below? Now the big question comes, Why should you do that?
The answer is to take advantage of Polymorphism. If you use an interface then in the future if the new implementation is shipped, then you are not required to change your program.
For example, an application written using List will work as expected whether you pass a LinkedList, Vector, or ArrayList because they all implement List interface, they obey the contract exposed by the List interface.
The only difference comes in performance, which is actually one of the drivers for change. In short, if you program using an interface, tomorrow if a better implementation of your interface is available then you can switch to that without making any further change on the client-side (part of the program which uses that interface). You can further see the Pyramid of Refactoring (Java) - Clean Code into Chain course on Udemy to learn about writing clean code in Java.
Similarly, you should use interface type on method arguments:
and use interface type on the return type of methods:
If you store ArrayList's object into a reference type of ArrayList, as shown below, then your code will not work if you pass a LinkedList, because ArrayList IS NOT a LinkedList.
Your program is pretty much hardcoded, it lacks the flexibility offered by Polymorphism and Inheritance. This answer is also applicable to questions like the difference between Map and HashMap or Set and HashSet because ultimately they are the same question, the difference between an interface and their implementation.
You must also follow SOLID Design Principles to write a better program in object-oriented programming languages like Java or C++.
Here are 10 object-oriented principles Every Java Developer should know
Other Java Collection Articles you may like
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback, please drop a note.
P. S. - If you are keen to learn Java Programming in-depth but looking for free online courses then you can also check out Java Tutorial for Complete Beginners (FREE) course on Udemy. It's completely free and you just need an Udemy account to join this course.
This is known as Polymorphism because any virtual method will be executed from subclass only, even though they were called from the supertype. This is the beginning, now let's see those two questions as well.
Anyway, If you are not familiar with Inheritance and other object-oriented programming concepts in Java, I suggest you first go through a comprehensive course like The Complete Java Masterclass, which is the most up-to-date online course to learn Java. Always updated for the latest Java version like Java 12.
Why store the ArrayList object on the List variable?
You might have seen something like this:List<Movie> listOfMovies = new ArrayList<Movie>()
Here we are using a List as a type of variable to store an object of ArrayList class, created using the new() operator. This is known as programming for the interfaces. In fact, everywhere you need to declare a reference variable, you should always use the supertype, like Map for passing HashMap, Set for giving HashSet, and List for passing ArrayList, LinkedList, or Vector.
You should use interface as type on the return type of method, type of arguments, etc. as shown below? Now the big question comes, Why should you do that?
The answer is to take advantage of Polymorphism. If you use an interface then in the future if the new implementation is shipped, then you are not required to change your program.
For example, an application written using List will work as expected whether you pass a LinkedList, Vector, or ArrayList because they all implement List interface, they obey the contract exposed by the List interface.
The only difference comes in performance, which is actually one of the drivers for change. In short, if you program using an interface, tomorrow if a better implementation of your interface is available then you can switch to that without making any further change on the client-side (part of the program which uses that interface). You can further see the Pyramid of Refactoring (Java) - Clean Code into Chain course on Udemy to learn about writing clean code in Java.
Similarly, you should use interface type on method arguments:
public void sort(List<? extends Comparable> input){ // logic to sort the list }
and use interface type on the return type of methods:
public List<Employee> getEmployees(int departmentId){ // logic to return employees for a given department }
If you store ArrayList's object into a reference type of ArrayList, as shown below, then your code will not work if you pass a LinkedList, because ArrayList IS NOT a LinkedList.
but following code will work, even if you pass ArrayList, Vector, CopyOnWriteArrayList or LinkedList, because it expects a List, which is an interface:public List<Movies> getMovies(LinkedList<String> actors){ // this code will not work if you pass ArrayList }
public List<Movies> getMovies(List<String> actors){ // this code will work if you pass ArrayList, Vector etc }
Your program is pretty much hardcoded, it lacks the flexibility offered by Polymorphism and Inheritance. This answer is also applicable to questions like the difference between Map and HashMap or Set and HashSet because ultimately they are the same question, the difference between an interface and their implementation.
You must also follow SOLID Design Principles to write a better program in object-oriented programming languages like Java or C++.
Here are 10 object-oriented principles Every Java Developer should know
Other Java Collection Articles you may like
- How to sort a Map by keys and values in Java? (tutorial)
- Top 10 Courses to learn Java for Beginners (best courses)
- How to sort an ArrayList in ascending and descending order in Java? (tutorial)
- Difference between ArrayList and HashSet in Java? (answer)
- Top 5 Courses to learn Java Collections and Stream (courses)
- What is the difference between TreeMap and TreeSet in Java? (answer)
- 10 Free courses to learn Java in-depth (courses)
- The difference between HashMap and ConcurrentHashMap in Java? (answer)
- 10 courses to learn Data Structure in-depth (courses)
- The difference between HashMap and LinkedHashMap in Java? (answer)
- 20+ basic algorithms interview questions for programmers (questions)
- Top 5 Courses to become full-stack Java developer (courses)
- The difference between Hashtable and HashMap in Java? (answer)
- Top 10 courses to learn Python for Beginners (courses)
- The difference between HashSet and TreeSet in Java? (answer)
- 7 Best Courses to learn Data STructure and Algorithms (best courses)
- 50+ Core Java Interview Questions and Answers (questions)
- The difference between ArrayList and LinkedList in Java? (answer)
- Top 5 Courses to learn Spring Framework in-depth (courses)
- The difference between Vector and ArrayList in Java? (answer)
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback, please drop a note.
P. S. - If you are keen to learn Java Programming in-depth but looking for free online courses then you can also check out Java Tutorial for Complete Beginners (FREE) course on Udemy. It's completely free and you just need an Udemy account to join this course.
Thanks a lot! I was looking for this answer for a lot of time.
ReplyDeleteBest description, but if you add some real life examples then it will be pretty easy to learn and remember.
Thanks for feedback, I think I have given the example here, isn't it clear? could you elaborate more what do you really want?
DeleteThanks a lot. It made me understand the concept of polymorphism better !!
ReplyDeleteBy far this is the most amazing explanation for this topic. I'm sincerely thankful to you.
ReplyDeleteThank you
Deletethank you, you have clarified everything well
ReplyDelete