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 trial

Java Inheritance in Java Inheritance in Java Everything Inherits from Object

Phoenix S.
Phoenix S.
2,456 Points

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

Hi 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?

3 Answers

Hi 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.
Phoenix S.
2,456 Points

That does make sense! Thank you for explaining it to me! :)

You're welcome :)

Phoenix S.
Phoenix S.
2,456 Points

I tried 4 different times

ok. Thanks

Thank 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 :)

You're welcome @Laura Owens :) I'm glad I could help.