I really like Joda-Time, but I've run into something I consider to be a problem. I'd like to extend some of the classes, specifically DateTime, LocalDate and LocalDateTime. But they are marked "final".
I found a very old thread where this is explained as being a way to ensure the classes remain immutable. http://osdir.com/ml/java-joda-time-user/2006-06/msg00001.html
I've also found a thread here on SO where the need to mark a Java class as final to ensure immutability is debated. Why would one declare an immutable class final in Java?
Anyway, I find it to be a major restriction that I can't extend these classes. Is there anything one can do, short of downloading the source files and modifying them, to create extended versions of these classes?
Edit - Discussion:
The ability to extend a class is one of the most powerful and useful concepts in object-oriented programming. This can always be useful. The author of a class can not possibly be 100% sure that his/her super-duper class will not be even more useful for some programmers when extended to cover use cases that nobody could foresee.
The apparent reason that the Joda-Time classes are marked "final" is to ensure that it is not possible for someone to create an extended class that is mutable, and use that with existing programs that are dependent on the Joda-Time objects being immutable. So to some extent the marking of these classes as "final" is due to the lack of a Java language mechanism that permits classes to be marked "immutable", so they could be extended, but only if the extended class is also marked "immutable".
So given the lack of an "immutable" keyword in Java, I can understand that the author of Joda-Time wants to avoid this situation.
Would the following solution be viable? Could we have a structure where, for example, LocalDate is derived from LocalDateNonFinal? LocalDate is an empty class that is marked "final". All functionality is in LocalDateNonFinal.
So if you really want to extend the LocalDate class, and only intend to use the extended class in your own programs, then you can instead extend LocalDateNonFinal, and call it MyLocalDate. This will not expose other modules to possible mistakes on your part because they will still require LocalDate, and won't accept LocalDateNonFinal or your MyLocalDate.
This could be combined with an attempt to educate programmers who want to extend these classes, warning them of the possible problems if they accidentally create a mutable version and still treat it as if it were immutable. And pointing out that these extended classes will not be usable with other modules that expect the regular ("final") classes.
PS. I'll post my work-around solution in a couple of days, when I'm completely sure. So far I've up-voted two of the answers - thanks for your comments and suggestions. I'm currently leaning towards a wrapper-like solution along the lines suggested by Dmitry Zaitsev.