Tuesday, November 4, 2008

Published 11/04/2008 by with 0 comment

missing method body, or declare abstract

If we put a ; (semicolon) after the method header (i.e., before starting the method body) when we are defining a concrete method (which method has body) then we will see “missing method body, or declare abstract”. Another reason is if we don’t write the keyword ‘abstract’ for an abstract method (a method without body), we’ll also see this message.
To resolve the problem we must remove the ; (semicolon) from the header of the concrete method or if it is an abstract method the keyword ‘abstract’ must be written in the method header.

Here is an example of this error:
public abstract class MissingMethodBodyOrDeclareAbstract
{
public void display(String message);
{
System.out.println(message);
}
public void display();
}

Here, a ; (semicolon) is given after the method header of the first display method (with parameter). And for the second display method, keyword ‘abstract’ is not stated in the header and it doesn’t have any body also. Here it is to be noted that if a class contains an abstract method, the class must also be abstract.

The correct code is:

public abstract class MissingMethodBodyOrDeclareAbstractSolution
{
public void display(String message)
{
System.out.println(message);
}
public abstract void display();
}
    email this       edit

0 comments:

Post a Comment