-5

I just want to know why we initialize object of a parent class and instantiate it with the child class instead of directly instantiating child class object.

Sample :

List<String> sampleList=new ArrayList<String>();

And Not

ArrayList<String> sampleList=new ArrayList<String>();
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
2787184
  • 3,749
  • 10
  • 47
  • 81

3 Answers3

4

Say I create a method that does something with an ArrayList:

public int sum(ArrayList<Integer> list){
   int sum = 0;
   for(Integer x : list){
      sum += x;
   }
   return sum;
}

That will work fine for now, and anybody with an ArrayList can call it. But what about people with a LinkedList? They're out of luck!

But why should I exclude them? My function doesn't actually use anything specific to ArrayList- I don't actually care or need to know what type of List the function is being given. So why not make it as general as possible, so anybody with a List can call my function?

public int sum(List<Integer> list){
   int sum = 0;
   for(Integer x : list){
      sum += x;
   }
   return sum;
}

Similarly, why should I declare a variable as an ArrayList if I don't actually care what type of List it is? What if I want to change that variable to a LinkedList later? If I declare it as a List, then that's a one-line change. But if I declared it as an ArrayList, then I have to change every single reference to it. That can become very annoying in larger projects, so it's usually better to keep your variables as general as possible.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
1

First of All List is an Interface not a Class. Secondly this is called Polymorphism, Polymorphism is an important concept in Object Oriented Programming.

A use case for using a reference to the Parent Class (or an Interface that is implemented by the Class), is if you want to use that reference to iterate through different Child Classes of that parent Class.

This thread might be of interest to you.

Community
  • 1
  • 1
Zaid Malhis
  • 588
  • 4
  • 18
1

Your question is wrong on at least the following counts:

  1. We don't "initialize object of a parent class" in the List<String> sampleList part—that's just type declaration.

  2. You cannot initialize object before you instantiate it.

  3. Actually we instantiate classes, not objects. The result of class instantiation is an object.

  4. List is not a "parent class", it is an interface. Interfaces cannot be instantiated/initialized at all.

If we eliminate all these errors from your question and rephrase as "Why do we declare a variable of a general type", then the answer is that it makes code more readable and maintainable: that way you promise you won't use any specifics of ArrayList, but will strictly use it as a general List. This becomes more important when used in method parameters: it makes your method more useful by allowing it to be used with any List implementation. This is actually what polymorphism is all about.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436