- Java classes are similar to C++ as described in the third paragraph. Java interfaces are a different concept.
- In Java, a class implicitly specifies both an interface in the GoF sense and the implementation. By contrast a Java interface explicitly specifies an interface in the GoF sense and doesn't specify an implementation. In both cases inheritance implies interface inheritance. Only in the case of class inheritance does it imply implementation inheritance.
For a better example of the distinction, I highly recommend learning just enough Ruby to understand how a class can use method_missing
to proxy off of an unrelated object. This provides a very explicit example of interface inheritance without implementation inheritance. (Bonus, Ruby's OO model is the same as Smalltalk's while having a more familiar syntax, which gives you a leg up on the rest of the book.)
Declaring immutable class final
saves programmer from the need to repeat declaring final in each and every method declaration, over and over and over again.
In classes like java.lang.String, having over 60 methods, this is substantial save, as well as important guarantee that necessary modifier won't be omitted by mistake.
When object is intended to be immutable, mistakes to declare final method may be hard to detect, because there is no reliable way to tell whether programmer omitted final modifier intentionally or by mistake.
final classes have no real purpose except perhaps brevity
Besides brevity and helping to avoid mistakes, a strong benefit of declaring immutable class final is that this makes programmer's intent explicit, unambiguously communicating to API users that none of class methods are intended for override.
Alternative way, that is, declaring all methods final, lacks this important "feature", as it leaves users of the API in the indecisive state, whether API designers intended to only protect some methods from overloading, and it only accidentally turned out that all of them got final, or there was a design decision to cover all the class.
my opinion is that final classes are superfluous as final methods are all you need
Given above, having final modifier for class doesn't look superfluous to me.
It is worth noting that Sun/Oracle Java tutorial presents final classes as a matter of usefulness and not as that of convenience. If final classes purpose was mere brevity / syntactic sugar, one would expect tutorial to explain these as convenience.
...you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String
class.
Best Answer
The general answer is no, because you should never add code without having a specific, concrete reason for it, and there is no general reason for such an interface.
That being said, sometimes there can be a good reason. But in all cases I have seen, these interfaces were partial, covering only one or a few properties shared by multiple classes I wanted to use polymporphically without giving them a common superclass. Typical candidates are an
Id
property to use in some sort of registry or aName
property to display to the user. But it can be useful in any case where you want some code to handle everything that has an X - just create anXSource
interface that contains thegetX
(and, only if required, thesetX
) methods.But a separate interface for every model class, containing all the properties? I can't imagine a good reason to do that. A bad reason would be a badly designed framework that requires it; Entity EJBs did just that, if I remember correctly. Thankfully they were so bad they never gained much traction and are deprecated since EJB 3.0
Sidenote: please avoid using the term "value object" to describe Java beans with only trivial getters and setters - it conflicts with the more common definition of value object as something with no identity that is usually immutable. A better term would be DTO or model class - though in the latter case note that anemic domain models are considered an antipattern.