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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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
“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.
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…
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.
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);
}
}
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?