Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Style question on abstract classes that implement interfaces

Thread view: 
c_kernel@hotmail.com - 10 Oct 2006 21:42 GMT
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!
Lothar Kimmeringer - 10 Oct 2006 22:53 GMT
> 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
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Mike Schilling - 11 Oct 2006 05:16 GMT
>> My question is twofold: 1.) Does the extra declaration in the
>> abstract class hurt anything (performance maybe...?
[quoted text clipped - 9 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.