Style question on abstract classes that implement interfaces

Discussion in 'Java' started by c_kernel@hotmail.com, Oct 10, 2006.

  1. Guest

    This may seem like an odd question, but please bear with me... Consider
    the common scenario where you have an interface, an abstract base class
    that implements some (but not all) of that interface, and a concrete
    derived class that implements the rest. In Java, unlike in C#, it is
    perfectly ok to omit some of the interface methods from the abstract
    base class declaration. They are considered to be implicitly declared
    "abstract" there. For example:

    interface IFoo
    {
    void bar();
    }

    abstract class FooBase implements IFoo
    {
    public abstract void baz();

    // This doesn't actually need to be here.
    // public abstract void bar();

    public final void goo()
    {
    System.out.println( "FooBase.goo()" );
    }
    }


    class Foo extends FooBase
    {
    public void baz()
    {
    System.out.println( "Foo.baz()" );
    }

    public void bar()
    {
    System.out.println( "Foo.bar()" );
    }
    }

    In this example, bar() doesn't need to be declared in FooBase. I have
    tried this out in a little test app both with and without the redundant
    declaration of bar(). Having it there doesn't seem to hurt anything,
    and it makes the class declaration more explicit.

    My question is twofold: 1.) Does the extra declaration in the abstract
    class hurt anything (performance maybe...? Although I can't see how...)
    2.) If not, which is preferred from a style point of view? Would it be
    considered very strange to have the redundant declaration there?

    Thanks!
     
    , Oct 10, 2006
    #1
    1. Advertising

  2. wrote:

    > My question is twofold: 1.) Does the extra declaration in the abstract
    > class hurt anything (performance maybe...?


    No. Only the class-file will be a little-bit bigger I assume.

    > 2.) If not, which is preferred from a style point of view? Would it be
    > considered very strange to have the redundant declaration there?


    I always add all methods in the abstract class and declare
    the unimplemented ones abstract, just to have it inside the
    Javadoc and to show that it's intended to be abstract and
    that it's not a forgotten implementation of a method that
    has been added to the interface at a later time.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Oct 10, 2006
    #2
    1. Advertising

  3. Lothar Kimmeringer wrote:
    > wrote:
    >
    >> My question is twofold: 1.) Does the extra declaration in the
    >> abstract class hurt anything (performance maybe...?

    >
    > No. Only the class-file will be a little-bit bigger I assume.
    >
    >> 2.) If not, which is preferred from a style point of view? Would it
    >> be considered very strange to have the redundant declaration there?

    >
    > I always add all methods in the abstract class and declare
    > the unimplemented ones abstract, just to have it inside the
    > Javadoc and to show that it's intended to be abstract and
    > that it's not a forgotten implementation of a method that
    > has been added to the interface at a later time.


    And also to allow creators of concrete subclasses find a list of all the
    methods they need to implement in one place.
     
    Mike Schilling, Oct 11, 2006
    #3
    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. Rhino
    Replies:
    32
    Views:
    1,278
    Thomas Hawtin
    Feb 11, 2006
  2. Replies:
    11
    Views:
    707
    Chris Uppal
    May 26, 2006
  3. malli
    Replies:
    4
    Views:
    592
    Manivannan Palanichamy
    Jul 8, 2007
  4. Replies:
    7
    Views:
    326
  5. sasha
    Replies:
    7
    Views:
    402
    thusa12
    Aug 5, 2008
Loading...

Share This Page