Style question on abstract classes that implement interfaces

C

c_kernel

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!
 
L

Lothar Kimmeringer

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: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Mike Schilling

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


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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top