FAQ: Inheritance and Polymorphism - Using a Child Class as its Parent Class

This community-built FAQ covers the “Using a Child Class as its Parent Class” exercise from the lesson “Inheritance and Polymorphism”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Using a Child Class as its Parent Class

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi Codecademy

Here are my questions:

  • Is is true that using a Child Class as its Parent Class (ParentClass childInstance = new ChildClass()) is just another way to create instance of a child class (ChildClass childInstance = new ChildClass())?
  • What are the real benefits of using this unusual way of creating a child instance compared to the normal way? As you mentioned in the lesson “using the explicit child as parent syntax is most helpful when we want to declare objects in bulk”, I still don’t get what you really mean

The course is superb and meticulously prepared by the way
Thanks

3 Likes

I had the same questions. I hadn’t seen this before.

After googling a bit I found this is referred to as “up casting”. The examples I saw involved saving instances of ChildClass in an array of ParentClass. When the objects are retrieved from the array they are treated as instances of ParentClass unless they are “down cast” to the ChildClass.

If the down cast is invalid (the object is not an instance of ChildClass) you get a ClassCastException. The getClass() method can be used to determine the actual class of the object so your code could avoid the exception.

1 Like

In no way am I trying to lecture anyone on solutions. I post these with my interpretations for personal reasons, mostly making sure that I’m understanding what I’m doing. My professor has mentioned many times that trying to explain a concept is the best way to see where the gaps in your own knowledge are.

Dinner class with notes

class Dinner {
  
  private void makeNoodles(Noodle noodle, String sauce) {
    
    noodle.cook();
    
    System.out.println("Mixing " + noodle.texture + " noodles made from " + noodle.ingredients + " with " + sauce + ".");
    System.out.println("Dinner is served!");
    
  }
  
  public static void main(String[] args) {
    
    Dinner noodlesDinner = new Dinner();
    // Add your code here:
    Noodle biangBiang = new BiangBiang();
    // BiangBiang is after the keyword "new" because that is the object we are        instantiating
    // Noodle is the parentclass relative to the BiangBiang class. One level higher.
    // The compiler will think the variable/reference "biangBiang" is just a "Noodle" object. However, at runtime it will create the "BiangBiang" object we set out to create. 
    
    // The overall superclass is the Dinner class where our main() method is stored. That is what our program is ultimately created for. The Noodle and the type of noodle is just one type of dinner possibility. 
    noodlesDinner.makeNoodles(biangBiang, "soy sauce and chili oil"); 
    
    
  }
  
}

Noodle class; nothing changed here. Still contains the fields, the constructor, and the one method it had from the outset.

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 void cook() {
    
    this.texture = "cooked";
    
  }
  
}

Nothing has changed in the child class BiangBiang either; same code that was given

class BiangBiang extends Noodle {
  
  BiangBiang() {
    
    super(50.0, 5.0, "flat", "high-gluten flour, salt, water");
    
  }
  
}