Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPhoenix S.
2,456 PointsI think there's a bug in the first Inheritance quiz
When answering questions 4 of the first inheritance quiz my answers were marked correct in the editor and wrong upon submission. It's not really a huge deal but I want to know what's going on.
The blanks I filled in will be noted with **
Here's the code for Question 4: How would you fix this code?
public class Main {
public static void main(String[] args) {
Thing widget = new Widget();
**((Widget)excuseId[1]) || ((Widget)excuseId[0]) || ((Widget)excuseId[]) || ((Widget)excuse)**.refuseToWork();
}
}
class Widget extends Thing {
String[] excuses = {"It's too heavy.", "I don't know how.", "You know I don't speak Spanish."};
int excuseId = 1;
void refuseToWork() {
String excuse = excuses[++excuseId];
System.out.println(excuse);
}
}
class Thing {
String purpose = "do stuff";
void printPurpose() {
System.out.println(purpose);
}
}
3 Answers
Lauren Moineau
9,483 PointsHi Phoenix. I think you got a green tick from the editor here because you cast the object, which was what was expected. However, I'm afraid your answer is indeed incorrect. When you write
((Widget)excuseId[1].refuseToWork();
you are actually calling the refuseToWork() method on an excuse String, expecting an excuse String as an answer.
To know which object you should call a method on, just check where the method is implemented. Here, the refuseToWork() method is implemented in the Widget class. So we'll need to call that method on a Widget object.
In the main method, we do have an object called widget but it's actually a Thing. So we will need to cast it as a Widget in order for us to call the refuseToWork() method on it.
Now, you were absolutely correct in writing a double parenthesis to ensure we cast the object we call the method on and not the result of that method. So we should have:
((Widget)widget).refuseToWork();
I hope this helps :)
Phoenix S.
2,456 PointsThat does make sense! Thank you for explaining it to me! :)
Lauren Moineau
9,483 PointsYou're welcome :)
Phoenix S.
2,456 PointsI tried 4 different times
Lauren Moineau
9,483 Pointsok. Thanks
Laura Owens
15,044 PointsThank you! I was struggling with that last night and re-watched the entire module but still didn't quite get it. Your answer is perfect :)
Lauren Moineau
9,483 PointsYou're welcome @Laura Owens :) I'm glad I could help.
Lauren Moineau
9,483 PointsLauren Moineau
9,483 PointsHi Phoenix. Was your answer made up of all that is between ** or did you try 4 different times with each of the 4 (widget) excuseId[] you put there?