#Why does this Anonymous Class test not work?
1 messages ยท Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
The Anonymous Class test does not work because the code is missing the closing curly brace for the interface declaration. The correct code should be:
public class App {
// Wohoo! App class!
interface HelloWorld {
public void greet();
public void greetSomeone(String someone);
}
public static void main(String[] args) {
HelloWorld hello = new HelloWorld() {
@Override
public void greet() {
System.out.println("Hello World!");
}
@Override
public void greetSomeone(String someone) {
System.out.println("Hello " + someone + "!");
}
};
hello.greet();
hello.greetSomeone("John");
}
}
In this code, an anonymous class is created that implements the HelloWorld interface. The greet() and greetSomeone() methods are overridden within the anonymous class, and then an instance of this anonymous class is created and used to call the methods.