Monday, May 9, 2011

Published 5/09/2011 by with 1 comment

interface methods cannot have body

Java error "interface methods cannot have body" occurs when body of the method within an interface is defined.

See the code below:

public interface MyInterface
{
public void myMetod()
{
}
}

Here the method myMethod has body starting at the symbol { and ending at }. An interface can have abstract methods (method without any body). The class that implements the interface defines the body of the interface methods.

So to remove the error, the method signature/header will end by ; (semi colon) and there will be no body section for the method. Below is the correct code:



public interface MyInterface
{
public void myMetod();
}


    email this       edit

1 comment: