简体   繁体   中英

Java default Interface Methods

Here is my simple code from a Java Tutorial.

public interface AnimalIntf {

    default public String identifyMyself(){
        return "I am an animal.";
    }

}

I get an error: illegal start of type interface methods cannot have body. The method is default and the default keyword is used at the beginning of the method signature. Could you please explain to me what is wrong?

Java 8中引入了默认接口方法,因此您需要一个支持Java 8或更高版本的JDK。

You must use Java 8 or above to have default implementations in interfaces. Instead you could use an abstract class. But even then, you wouldn't use the default keyword.

Strange things are happening... After the JDK update to version 8 both on computer and in IDE settings, the code compiles OK, but the IDE still marks the line

default public String identifyMyself(){

as error. And there were still errors while trying to use the interface, asking to override the default method in the class that implements the interface.

public class Dragon implements Animal{    
}

2 hours later I got tired of trying to fix my NetBeans v.6.9.1 and downloaded NetBeans v8.0.1. Now I am fine :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM