Efi said:
Hi,
I started using CheckStyle in my projects and one of the erros that I
encountered was
"method is not designed for extension" i.e. method declaration should
be either empty, final or abstract.
Those are three very different things. So the "i.e. [sic]" makes no sense.
Final and abstract methods are pretty much the opposite of each other.
Abstract certainly means "designed for extension", in fact, "required to be
extended". Final means not allowed to be extended, the antithesis of abstract.
One might say "final" means "designed not to be extended". Empty merely means
no action occurs in the body, and can be either final or not.
What does Checkstyle's documentation say about this particular message?
Certainly not that the method "should be either empty, final or abstract".
I remember reading about this subject that sub classing a class that
was not supposed to be sub classes is a dangerous thing.
Turn that on its head. Allowing a class to be subclassed without taking
certain precautions is a dangerous thing.
Furthermore, even when the mechanics of extension are correct, there is a
modeling issue. Inheritance models an "is-a" relation: an object of the child
type "is-a" thing of the parent type also. All too often, people create
inheritance hierarchies where the model does not call for an "is-a" relation.
Joshua Bloch covers these topics admirably in /Effective Java/.
My question is how you as a developer knows when your class will be
subclasses, that is when you write your classes you never know how
people will use them in the future, so preventing people from sub
classing is counter productive.
Au contraire! Preventing a class from being inherited is highly productive, if
the model calls for it. A developer does know certain things about how people
will use their API - all those things that the developer was careful to ensure
in the API "contracts", i.e., the method signatures. So, for example, if you
declare a class final then you as the developer do know, for a fact, that no
one will subclass it. If you declare a non-final class method to be final,
then you as the developer know, for a fact, that no subclass of your class
will override that method. These can be very useful restrictions to place on
clients of your API.
The secret is that the developer knows exactly how their API will be used,
because the developer is the very one who defines how their API will be used.
What the developer cannot know, or needs to care about, is where their API
will be used.
A subtle but significant point.
Google "design by contract".
- Lew