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 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.java:5: error: class, interface, or enum expected
}
I am unable to understand these errors. How can I correct the problems in my code?