[newbie] Can an abstract method have a signature?

Discussion in 'Java' started by Wes Harrison, Aug 2, 2004.

  1. Wes Harrison

    Wes Harrison Guest

    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
    Wes Harrison, Aug 2, 2004
    #1
    1. Advertising

  2. Wes Harrison

    ShadowMan Guest

    "Wes Harrison" <> ha scritto nel messaggio
    news:RLqPc.154$
    > 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! ;-) )

    --
    ShadowMan
    ShadowMan, Aug 2, 2004
    #2
    1. Advertising

  3. "Wes Harrison" <> a écrit dans le message de
    news:RLqPc.154$...
    > 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);
    Olivier Merigon, Aug 2, 2004
    #3
  4. Wes Harrison

    jAnO! Guest

    "Wes Harrison" <> wrote in message
    news:RLqPc.154$...
    > 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 ;
    jAnO!, Aug 2, 2004
    #4
  5. Wes Harrison

    Oliver Fels Guest

    Wes Harrison wrote:

    > 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
    Oliver Fels, Aug 2, 2004
    #5
  6. Wes Harrison

    Cid Guest

    On Mon, 02 Aug 2004 17:05:33 +0200, Oliver Fels <>
    wrote:

    >Wes Harrison wrote:
    >
    >> 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.
    >


    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. (?)
    Cid, Aug 3, 2004
    #6
  7. Wes Harrison

    Oliver Fels Guest

    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
    Oliver Fels, Aug 3, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,041
    Joerg Jooss
    Apr 24, 2004
  2. Sameer
    Replies:
    4
    Views:
    572
    Roedy Green
    Aug 31, 2005
  3. Chris Zopers

    Abstract method in non-abstract class

    Chris Zopers, Aug 8, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    357
    Göran Andersson
    Aug 8, 2008
  4. Gustavo Narea
    Replies:
    14
    Views:
    847
    Gustavo Narea
    Feb 16, 2009
  5. Stefan Ram
    Replies:
    27
    Views:
    712
    Stefan Ram
    Aug 17, 2009
Loading...

Share This Page