FAQ: Inheritance and Polymorphism - Inheritance in Practice

This community-built FAQ covers the “Inheritance in Practice” 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 Inheritance in Practice

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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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!

This is regarding the question in the 2nd exercise, where the parent class is ‘Noodle’ and the child class is ‘Spaghetti’.
How were we able to create an object (spaghettiPomodoro) without defining its constructor in the spaghetti class? All we did was define a class. No constructor was defined.
I searched on google and confirmed that we cannot create an object without a constructor. Is it a mistake in the IDE or am I missing something?

Java provides a default constructor in the case if do not explicitly add one. And yes you are right about object cannot be created without constructor but the java itself defines default constructor without any parameters

1 Like

What is the code this.texture means and perform in the second exercise in line 9?

“this.” refers to the class instance in question. You may have several types of spaghetti in your program, but want the texture of a specific instance.

If you’re familiar with Python, this. is analogous to self.

2 Likes

I’m stuck on step 3. How do I print the String, “texture,” if it’s already stated.

You are asked to create an instance of Spaghetti class in Noodle class’s main method.

Spaghetti yourNewInstance = new Spaghetti();

Each new instance of a class is an object which receives states defined in its class and its class’s parent class (inheritance). All those states become properties of each instance object. You can access them like properties.

<object>.<property>;

So if Spaghetti class inherits all states and methods of its parent class (Noodle), your new instance object should have a property called ‘texture’. You can access it like this…

yourNewInstance.texture;

To print it wrap it in System.out.println().

1 Like

Can anyone help me? I’m confused. How can I call a child class from a parent one? Shouldn’t it be the other way around? (I’m talking about Step 3)

I also had the same worry. Anyway, as I know it’s impossible to create an object without first creating a constructor, I started out by creating one. And only after this went back to Noodle main class and created the Spaghetti object.

It worked, of course.

I was just wondering why the task didn’t say anything about the need to create a constructor first in the Spaghetti class.

I read your answer to a similar question-you said Java creates a default constructor-but so far in this course nothing has been said about this feature. It explicitly said that you always need a constructor to make an object.

Pat

I have the same exact question! I see your post was from three months ago, please let me know if you found the answer I am so confused.

Hi folks!

I got also the problem. Here is the solution:

class Noodle {
  
  double lengthInCentimeters;
  String shape;
  public static String texture = "brittle";  // make this to public otherwise you get en error
  
  public void cook() {
    this.texture = "cooked";
  }
  
  public static void main(String[] args) {
    Spaghetti spaghettiPomodoro = new Spaghetti();

    System.out.println(texture);
  }
  
}

This works also, but I don’t find the solution that texture is getting “cooked”

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);
  }
  
}

Same here. I dont get why we made an object of a child class inside the parent class. After explaining that we are learning the opposite. I would expect the main() and the method or variable from the parent to be used in the child. In this example its parent using child. Can someone shed light on this?

me too, same question. this is the opposite flow they are teaching.