FAQ: Inheritance and Polymorphism - Child Classes in Arrays and ArrayLists

This community-built FAQ covers the “Child Classes in Arrays and ArrayLists” 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 Child Classes in Arrays and ArrayLists

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!

My solution to the problem 8/9:

// Add your code below:
    Noodle[] allTheNoodles = {spaghetti, ramen, pho};
    
    for (Noodle noodle : allTheNoodles) {
      System.out.println(noodle.getCookPrep());
    } // end for-each loop
3 Likes

I got the right answer just following the syntax in the example, but I don’t quite understand the usage of the colon ( : ), and I don’t believe it was explained earlier. Can someone please walk me through it? I can kind of infer how it works, but would appreciate someone who actually knows explain it so I don’t get it wrong. Thanks!

I think it is explained in the for-each lesson of loops. The syntax(element:b) means that ‘for the element in a list of elements in b’ in which it does an operation or something to each element in b. Like the above example~
for (Noodle noodle : allTheNoodles) {
System.out.println(noodle.getCookPrep());
}
infers that for each noodle in allTheNoodles (do something to each noodle, print etc)!

2 Likes

There’s this section in the instructions:

Monster dracula, wolfman, zombie1;

dracula = new Vampire();
wolfman = new Werewolf();
zombie1 = new Zombie();

Monster[] monsters = {dracula, wolfman, zombie1};

I don’t understand the syntax at the top, or what its function is. Does it set dracula, wolfman, and zombie1 as Monster types? Can this code run without that line?

1 Like

Thanks for explaining it! I have been looking at a few other resources (after having completed the Codecademy Java lessons) and it cemented what I had originally inferred. I think I originally was confused as “noodle” was not defined anywhere else and the syntax just threw me for a bit of a loop (no pun intended). Thanks again for explaining it, much appreciated!!

1 Like

Under the subject Inheritance, can someone please advise me how I can create a child class (file) that compiles in IntelliJ IDEA? I do not have problems with the code. My problem is in linking the Child to the Parent and achieve compilation.

Below is the code for the 2nd lesson on Inheritance:
class Noodle {

double lengthInCentimeters;
String shape;
String texture = “brittle”;

public void cook() {

this.texture = "cooked";

}

public static void main(String args) {

Spaghetti spaghettiPomodoro = new Spaghetti();

System.out.println(spaghettiPomodoro.texture);

}

}
I googled for a solution, the only advise that was close to what I needed was from StackOverflow, and I qoute:
" If you are already in the Project View, press Alt+Insert ( New ) | Class . Project View can be activated via Alt+1.

To create a new class in the same directory as the current one use Ctrl+Alt+Insert ( New… ).

You can also do it from the Navigation Bar, press Alt+Home, then choose package with arrow keys, then press Alt+Insert.

Another useful shortcut is View | Select In (Alt+F1), Project (1), then Alt+Insert to create a class near the existing one or use arrow keys to navigate through the packages.

And yet another way is to just type the class name in the existing code where you want to use it, IDEA will highlight it in red as it doesn’t exist yet, then press Alt+Enter for the Intention Actions pop-up, choose Create Class ."
I tried this: ctrl + alt + insert. This took me to a GUI where I was asked to name the class, and, in addition, choose from “Class”, “Interface”, “Enum” and “Annotation”. For name I typed in “Spaghetti” and for “Kind”, I selected “Class”. This created a seemingly child class “Spaghetti” as a sub-file to “Noodle”. I was happy as there were no red squiggles in my code, but my happiness was short-lived as compilation failed. Can someone tell me what am I doing wrong?

Thanks guys. I made a breakthrough. Simply press Ctrl + Alt + Insert, name your child class, and select “class”. Back to your main IDEA environment, highlight parent file choose compile (Parent file name) and you are done. The trick was on which file to compile. DO NOT compile the child file, but the parent file.

Hi! in the line of code where we have
for (Noodle noodle : allTheNoodles) {}

how can we use “noodle” in the for-each loop if it hasn’t been defined yet?

You’re not exactly defining anything here. You’re basically just telling the computer to make noodle equal whatever item in the list the loop is currently on.

1 Like

Nice, thank you for explaining this. I was looking for the same thing.

It is the same as following:

Monster dracula = new Vampire();
Monster wolfman = new Werewolf();
Monster zombie1 = new Zombie();

You can also declare them as
Vampire dracula = new Vampire();
Werewolf wolfman = new Werewolf();
Zombie zombie1 = new Zombie();

You are essentially casting each member of allTheNoodles to a Object Type on the fly.