Skip to main content
added 140 characters in body
Source Link
Jason C
  • 40.6k
  • 16
  • 136
  • 201

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?

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 showed 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 } please help find the reason,thank you!

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?

Source Link

default method in interfaces in java

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 showed 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 } please help find the reason,thank you!