2

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?

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Yuliana
  • 31
  • 1
  • 3
  • What JDK version are you using? – Jeffrey Bosboom Oct 23 '14 at 19:46
  • @Jeffrey Bosboom, JDK 1.6 – Yuliana Oct 23 '14 at 19:48
  • 7
    You need JDK 8 to use default methods. – Jeffrey Bosboom Oct 23 '14 at 19:49
  • Surprisingly, you seem to be the first person to ask this question (or maybe the SO search is so useless it can't find the duplicate). – Jeffrey Bosboom Oct 23 '14 at 19:57
  • It would be interesting to know what kind of tutorial is was. Does it really introduce Java 8 features to the audience failing to tell that it aims at Java 8? – Holger Oct 24 '14 at 08:25
  • By the way `Java 6` aka `JDK 1.6` has it’s official “end of life” reached at [Feb 2013](http://www.oracle.com/technetwork/java/eol-135779.html#Java6-end-public-updates). You shouldn’t start learning Java with an outdated version. – Holger Oct 24 '14 at 08:33
  • @Yuliana I think folks here have adequately answered your question. If you agree, please "Accept" an answer (doesn't have to be mine). – JustinKSU Oct 24 '14 at 14:11
  • @Holger, It is Oracle's Java Tutorial: http://docs.oracle.com/javase/tutorial/java/IandI/ – Yuliana Oct 24 '14 at 23:18
  • @JustinKSU, you are right, thanks, didn't know about the 'accept' – Yuliana Oct 24 '14 at 23:20
  • 1
    Well, Oracle’s tutorial says it [on the start page](http://docs.oracle.com/javase/tutorial/): “*The Java Tutorials primarily describe features in Java SE 8. For best results, [download JDK 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html)*” – Holger Oct 27 '14 at 09:31

3 Answers3

5

Default interface methods were introduced in Java 8, so you need a JDK that supports Java 8 or later.

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
1

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.

JustinKSU
  • 4,875
  • 2
  • 29
  • 51
  • Oh, you meant if you're using an abstract class instead, you don't use `default` there. Sorry about that. – Jeffrey Bosboom Oct 23 '14 at 20:07
  • 1
    I have found this, the link could be helpful while determining which one to use, an interface or an abstract class: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html – Yuliana Oct 24 '14 at 23:13
0

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 :)

Yuliana
  • 31
  • 1
  • 3