0
Please help me ( what is the Error here )
Import java.lang.override; Public static void main(String[] args) { GreetingMessage gm = new GreetingMessage(){ @override Public void greet(String name) { System.out.println(âHello â + name); } }; gm.greet(âTheoâ); }
2 Answers
+ 4
There are several errors here, so, I'll post some working code for you to look at and a link on inline/anonymous classes for you to review. Also, note that an override isn't needed in this case, and you don't need to import override as it is implicitly already imported in all java programs.
public interface GreetingMessage {
public void greet(String name);
}
public class Program
{
public static void main(String[] args) {
GreetingMessage gm = new GreetingMessage(){
public void greet(String name) {
System.out.println("Hello " + name);
}
};
gm.greet("Theo");
}
}
https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
+ 2
thanks bro