[newbie] Can an abstract method have a signature?

W

Wes Harrison

I am getting an error about abstract methods not being allowed to have a
body but all I am doing is declaring the method's signature in an abstract
class like:

public abstract String format(String s, int i)
{
}


Wes
 
S

ShadowMan

Wes Harrison said:
I am getting an error about abstract methods not being allowed to
have a body but all I am doing is declaring the method's signature in
an abstract class like:

public abstract String format(String s, int i)
{
}

the right way should be without braces (empty braces ARE braces! ;-) )
 
O

Olivier Merigon

Wes Harrison said:
I am getting an error about abstract methods not being allowed to have a
body but all I am doing is declaring the method's signature in an abstract
class like:

public abstract String format(String s, int i)
{
}


Wes

Declare your method like this:

public abstract String format(String s, int i);
 
J

jAnO!

Wes Harrison said:
I am getting an error about abstract methods not being allowed to have a
body but all I am doing is declaring the method's signature in an abstract
class like:

public abstract String format(String s, int i)
{
}
No, you're also implementing the method.
Try to remove the body... hint {}... replace them for a ;
 
O

Oliver Fels

Wes said:
I am getting an error about abstract methods not being allowed to have a
body but all I am doing is declaring the method's signature in an abstract
class like:

public abstract String format(String s, int i)
{
}

No abstract bodies are allowed.
It is good practice to declare the abstract method in an interface:
public String format(String s, int i);

and let your (abstract) class implement it so you don't have to care much
about it.

The advantage: You can even implement a generic implementation in your
(abstract) class being overriden by children if necessary so you have a
fallback method for children which do not need the method themselves and
handle it in a generic way thus avoiding the NoSuchMethodException.

Oliver
 
C

Cid

No abstract bodies are allowed.
It is good practice to declare the abstract method in an interface:
public String format(String s, int i);

and let your (abstract) class implement it so you don't have to care much
about it.

When a method is declared abstract, it must not have a body and the
class containing it must also be declared abstract.

If the class is declared abstract but none of its methods are
abstract, then they must have bodies. But the class itself cannot be
instantiated. This presumably is what Oliver is referring to. (?)
 
O

Oliver Fels

Cid wrote:

When a method is declared abstract, it must not have a body and the
class containing it must also be declared abstract.

If the class is declared abstract but none of its methods are
abstract, then they must have bodies. But the class itself cannot be
instantiated. This presumably is what Oliver is referring to. (?)

Right.
To make it clear, two examples:

public abstract class A
{
public void methodA {...}; //not abstract
public abstract void methodB();
}


public interface B
{
public void methodB(); //abstract interface declaration
public void methodC(); //abstract interface declaration
}

public abstract class C implements B
{
public void methodB {...}; //not abstract and implements as default
behavior
}


In class C methodB is implemented with a default behavior which can be
overridden by children with a class specific implementation if required.
methodC *must* be implemented.
I mostly use this approach as it is flexible, though it requires more
discipline as forgetting the concrete chield implementation of methodB is
not immediately noticed at compile time..

Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top