Rzeźnik said:
I meant "omniscient", my mistake.
If you are ominous [?] then this idea is great. I'd bet you are not. In
fact declaring everything as final is worse than most things you can
do to cripple program development. IMO every time you are
_introducing_ final you have to think deeply.
Every time you omit 'final' in a class declaration you should consider
carefully. Read the item in /Effective Java/ referenced upthread for a
thorough explanation of why to prefer non-heritability.
If there is such guidance there then the book is useless rubbish - but
I can't say for sure because I actually did not read this.
As an API writer (that is, one who writes a class intended for use) one should
control how the API is used, not predict it.
Agreed. So if we are on the same page - we do you try to predict how
your API will be used and 'contaminate' it with 'final's?
Point being, if you mark a class as final, there's no more prediction
involved - you have laid down the law.
You can get mixed messages about using final for classes depending on
who you read. For example, Goetz in 2002
(
http://www.ibm.com/developerworks/java/library/j-jtp1029.html) does not
mention composition once, and since the use of final on classes can
often be a deliberate decision to discourage use of inheritance and
promote composition, it's an odd omission. What he does mention about
the use of final to enforce immutability is quite right, but I believe
he's incorrect when he states that the use of final on classes
discourages OO design. I think it's the other way around.
Nobody is saying that inheritance is evil, just that it should be
considered carefully when designing a class. It's not acceptable for
starters to *not* think about it: both the decision to use the final
keyword, and to not use it, should be thought out. It shouldn't be a
default. Goetz in 2002, and from the sounds of it you at present, seem
to fall into the camp of "don't mark a class as final unless you've got
really good reasons to do it", whereas I fall into the camp of "don't
_not_ mark a class as final unless you can explain why you want that
class to be a base class."
Inheritance is certainly not a bad thing, when properly used. I just ran
some stats on a medium-sized project I am helping to maintain, and out
of 2849 classes roughly a quarter of them (657 to be precise) extend
another. The majority of them are good uses of inheritance:
- creating custom exception classes (although I know that in some cases
this was overly enthusiastic);
- extending core classes that are meant to be so extended when doing JSF
customizations;
- concrete jobs that extend an abstract job that implements the Quartz
Job interface;
- all the JPA entities extend a @MappedSuperclass;
- concrete serializer classes that extend a default serializer base
class, the whole family meant to handle different types of inventory
serial numbers;
- concrete realizations of base classes (default or abstract) that
provide commonality for managed beans in this application.
And so forth. Bear in mind too that when Bloch and others warn about
inheritance they are referring to implementation inheritance, and where
the extensible classes are being used across package boundaries (IOW,
probably not by the same people who wrote the base classes), and where
the extensible classes are neither explicitly designed nor commented for
inheritance. If all of those conditions obtain then you could have problems.
AHS