#Question help

8 messages · Page 1 of 1 (latest)

hexed temple
#

I'm supposed to write a class sky with a method toString, and a subclass cloud with a method toString that overwrites the original one, and then a detect class to check if the variable is from the class sky or clouds, but it does not work. Please help:

class Sky{
    
    String toString() {
        return "sky";
    }
    
}

class Cloud extends Sky{
    
    String toString() {
        return "cloud";
    }
}

void detect(Sky x)  {
    
    if (x.toString().equals("sky")){
        System.out.println("Got a sky!");
    } else if (x.toString().equals("cloud")){
        System.out.println("Got a cloud!");
    }
    
}
pale rapidsBOT
#

This post has been reserved for your question.

Hey @hexed temple! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

obtuse wharf
#

"does not work" doesn't help us help you

#

toString should be public, btw

hexed temple
#

thanks