Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Style question on abstract classes that implement interfaces

Reply
Thread Tools

Style question on abstract classes that implement interfaces

 
 
c_kernel@hotmail.com
Guest
Posts: n/a
 
      10-10-2006
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!

 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      10-10-2006
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!
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      10-11-2006
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Question about Abstract classes and Interfaces imenalo@menalo.ca Java 7 12-12-2007 03:20 AM
Question on abstract classes versus interfaces mdinino@gmail.com Java 11 05-26-2006 11:27 AM
Converting constants-only interfaces to abstract classes Rhino Java 32 02-11-2006 12:13 PM
Abstract Classes w/o abstract methods DaKoadMunky Java 4 04-20-2004 04:53 AM



Advertisments