Question regarding polymorphism - Using a Child Class as its Parent Class

Can anyone expand upon this?

Using the explicit child as parent syntax is most helpful when we want to declare objects in bulk.)

Some context is given below. What would be an example of declaring child objects in bulk and why is it easier if done explicity?

Thank you :smiley:

https://www.codecademy.com/courses/learn-java/lessons/java-inheritance-and-polymorphism/exercises/using-child-class-as-parent-class

BankAccount kaylasAccount = new CheckingAccount(600.00);

  • We can use kaylasAccount as if it were an instance of BankAccount, in any situation where a BankAccount object would be expected. (This would be true even if kaylasAccount were instantiated as a CheckingAccount, but using the explicit child as parent syntax is most helpful when we want to declare objects in bulk.)

This question is a bit old now, but it is a really good question, so I wanted to answer it anyways in case it helps anyone. (Don’t hurt me, mods!)

A simple example similar to declaring objects in bulk would be creating an ArrayList or another data structure that will store all bank accounts. You could declare the List like so:
List<BankAccount> bankAccounts = new ArrayList<>();

Every CheckingAccount is a valid BankingAccount, so all CheckingAccounts can be added to the list with no problems. The list can contain a mix of all different types of bank accounts as long as each account type extends the BankAccount class.

If you declared the list like this: List<CheckingAccount> list = new ArrayList<>();
Then you are limited to only adding CheckingAccounts and attempting to add a BankAccount will throw an error.

Hello,
Thank you for answering, but there is one thing that i don’t understand.
what is the benefit of instantiating like this :

BankAccount kaylasAccount = new CheckingAccount(600.00);

instead of

CheckingAccount kaylasAccount = new CheckingAccount(600.00);

For exemple in this exercise : https://www.codecademy.com/courses/learn-java/lessons/java-inheritance-and-polymorphism/exercises/child-classes-in-arrays-and-arraylists

I first did the exercise as requested :

class Noodle {
  
  protected double lengthInCentimeters;
  protected double widthInCentimeters;
  protected String shape;
  protected String ingredients;
  protected String texture = "brittle";
  
  Noodle(double lenInCent, double wthInCent, String shp, String ingr) {
    
    this.lengthInCentimeters = lenInCent;
    this.widthInCentimeters = wthInCent;
    this.shape = shp;
    this.ingredients = ingr;
    
  }
  
  public String getCookPrep() {
    
    return "Boil noodle for 7 minutes and add sauce.";
    
  }
  
  
  public static void main(String[] args) {
    
    Noodle spaghetti, ramen, pho;
    
    spaghetti = new Spaghetti();
    ramen = new Ramen();
    pho = new Pho();
    
    // Add your code below:
    Noodle[] allTheNoodles = {spaghetti,ramen,pho};
    for (Noodle noodle : allTheNoodles){
      System.out.println(noodle.getCookPrep());
    }
    
  }
  
}

Then I changed the instantiation of the three variables like this :

lass Noodle {
  
  protected double lengthInCentimeters;
  protected double widthInCentimeters;
  protected String shape;
  protected String ingredients;
  protected String texture = "brittle";
  
  Noodle(double lenInCent, double wthInCent, String shp, String ingr) {
    
    this.lengthInCentimeters = lenInCent;
    this.widthInCentimeters = wthInCent;
    this.shape = shp;
    this.ingredients = ingr;
    
  }
  
  public String getCookPrep() {
    
    return "Boil noodle for 7 minutes and add sauce.";
    
  }
  
  
  public static void main(String[] args) {
    
    //Noodle spaghetti, ramen, pho;
    
    Spaghetti spaghetti = new Spaghetti();
    Ramen ramen = new Ramen();
    Pho pho = new Pho();
    
    // Add your code below:
    Noodle[] allTheNoodles = {spaghetti,ramen,pho};
    for (Noodle noodle : allTheNoodles ){
      System.out.println( noodle.getCookPrep());
    }
    
  }
  
}

The output being the same, I don’t understand the advantage of instantiating spaghetti,ramen,pho as Noodle instead of Spaghetti for spaghetti for example, Ramen for ramen…

I was looking this up before too and I thought I might be able to provide some clarity. One of the benefits of declaring spaghetti, ramen and pho as Noodle instead of their specific class types (Spaghetti, Ramen, Pho) is that you gain flexibility in case you ever needed to change the implementation of the parent class.

This seemed more apparent to me with regards to declaring Lists. Suppose you have the following declaration of a List.
List myList = new ArrayList<>();

If you determined at a later stage that a LinkedList is more suitable than ArrayList for the operation you want to
perform, you could simply change the declaration without breaking other codes that rely on myList being a List:

List myList = new LinkedList();

ps. I’m relatively new in Java so I hope anyone more advanced can chip in or let me know if I’m mistaken in this application.

1 Like

great answer!

I also think the flexibility becomes more obvious when using interfaces instead of only parent classes, but perhaps that is a topic for another day.

1 Like

Ah yes that’s right. I didn’t even consider it in that context. Today I learned :smiley: Thanks davejandrea!