I am attempting to understand default methods in interfaces in Java using the following code, but I am unable to compile it:
public interface A { default void foo(){ System.out.println("Calling A.foo()"); } } public class Clazz implements A { } class c { public static void main(String[]args) { Clazz clazz = new Clazz(); clazz.foo(); // Calling A.foo() } } the code was used to understand default methods in interfaces of java,the code was not compiled,command prompt showedThe compiler produced the following output: c.java:2: error: illegal start of type default void foo(){ ^ c.java:2: error: = expected default void foo(){ ^ c.java:2: error: ';' expected default void foo(){ ^ c.java:2: error: illegal start of type default void foo(){ ^ c.java:2: error: expected default void foo(){ ^ c.java:2: error: = expected default void foo(){ ^ c.java:2: error: ';' expected default void foo(){ ^ c.java:3: error: illegal start of type System.out.println("Calling A.foo()"); ^ c.java:3: error: = expected System.out.println("Calling A.foo()"); ^ c.java:3: error: expected System.out.println("Calling A.foo()"); ^ c.java:3: error: illegal start of type System.out.println("Calling A.foo()"); ^ c
c.java:2: error: illegal start of type default void foo(){ ^ c.java:2: error: = expected default void foo(){ ^ c.java:2: error: ';' expected default void foo(){ ^ c.java:2: error: illegal start of type default void foo(){ ^ c.java:2: error: expected default void foo(){ ^ c.java:2: error: = expected default void foo(){ ^ c.java:2: error: ';' expected default void foo(){ ^ c.java:3: error: illegal start of type System.out.println("Calling A.foo()"); ^ c.java:3: error: = expected System.out.println("Calling A.foo()"); ^ c.java:3: error: expected System.out.println("Calling A.foo()"); ^ c.java:3: error: illegal start of type System.out.println("Calling A.foo()"); ^ c.java:5: error: class, interface, or enum expected } I am unable to understand these errors.java:5: error: class, interface, or enum expected } please help find How can I correct the reason,thank you!problems in my code?