Why in Java, we need to create Reference of Parent Class and Object of Sub class? What do we achieve with this? Obvious answer is Polymorphism, but then what is the scope? How does it help in real time? I came across this while learning java.
-
I assume you are talking about the paradigm: List
foo = new ArrayList – Trasvi Feb 21 '13 at 05:21(); ? -
I guess you want to know how does Polymorphism helps ? – Abdullah Shaikh Feb 21 '13 at 05:26
-
1check these. [answer1](http://stackoverflow.com/a/12159803/2024761), [answer2](http://stackoverflow.com/a/2482808/2024761) – Rahul Feb 21 '13 at 05:34
-
JDBC is an best example for that. You can define your own superclass; let all the drivers(oracle / mysql) should implement those. So if add any library you don't need to think about how the subclass (driver) is implemented. – Kanagavelu Sugumar Feb 21 '13 at 05:43
2 Answers
In your example, by using a parent class reference, you can write a method that takes any kind of List and operates on it, without caring about the specific type of list that it is being passed.
On the other hand, the subclass specifies the actual implementation, so the user of the method can choose whatever list is most efficient or appropriate for his requirement.
Suppose you write a method that computes the sum of a List:
int sum(List<Integer> input);
Now suppose one user of your method has a list in which he cares about fast random access, so he creates an ArrayList, while another user wants to frequently splice and join his lists, so he uses a LinkedList instead.
Both these users can now use your sum method.

- 71,677
- 44
- 195
- 329
How does polymorphism help you in real life?
By increasing the flexibility of your code to accommodate different arguments or implementations.
For example, one could think of a method:
void foo(ArrayList<T> arg) {
for (T t : arg) {
// do something
}
}
Do we really need to restrict this method to only accepting an ArrayList? After all, the function we're using there could equally well be performed on a LinkedList, or a Set. It would be even more functional if we instead wrote:
void foo(Iterable<T> arg) {
for (T t : arg) {
// do something
}
}
Similarly, if we write:
ArrayList<T> arg = new ArrayList<T>();
This is only useful if the methods we want to use in arg are only available to ArrayList. If the methods we want are declared in superclass of ArrayList, then we can make our code more flexible by changing it to:
Collection<T> arg = new ArrayList<T>();
And then, if we decide that arg in fact needs to be sorted, we could change it quite easily to
Collection<T> arg = new PriorityQueue<T>();
without breaking anything else in our code, because foo() will accept a PriorityQueue just as happily as it will accept an ArrayList. If you stop relying on specific implementation details for any subclass, you can change lots very quickly.
As mentioned in the comments, once you break it out of very simple examples it gets more powerful. Lets say we have an application which interfaces with a database. If our database is MySQL, you might get caught up in writing very specific MySQL code: (note, I haven't done any JDBC stuff in forever so this is completely contrived)
MySQL db = new MySQL();
int results = db.mySqlExecute("SELECT count(*) FROM names WHERE firstname = 'Bobby';");
After a lot of those in a big application, your code is completely inextricable from MySQL and you can't implement another database system like PostgreSQL or whatever. If instead you write:
DataBase db = new MySQL();
int results = db.getCount("names", "firstname = 'Bobby'");
and you can easily change the first line to
DataBase db = new PostgreSQL();
and your code will work great with just one simple change. That is the power of polymorphism: not being tied to a single implementation.

- 1,207
- 1
- 15
- 23