Suppose I've a utility class which contains only static methods and variables. e.g:
public abstract final class StringUtils
{
public static final String NEW_LINE = System.getProperty("line.separator");
public static boolean isNotNullOrSpace(final String string)
{
return !(string == null || string.length() < 1 || string.trim().length() < 1);
}
}
In this scenario, it makes sense to make the class both abstract and final. Abstract because making an object of this class will be of no use as all methods are accessible statically. Final because the derived class cannot inherit anything from this class as it does not have any non-static member.
C# allows static modifier for such classes. Why doesn't Java support this?
Due to some certain reasons we can not final use final with abstract class. 1. If we define abstract final then we can't extend it. 2. If we are defining abstract class as final the it will give the compile time error..
In Java an instance of an abstract class cannot be created, we can only have references of abstract class type. So there if we make abstract class final then we wont be able to extend it. abstract and final are the mutual exclusive concept. that's why Java compiler throws a compile time error when you try to make an abstract class final in Java
A
final
class can't be extended, anabstract
class needs to be extended in order to be instantiated. Therefore, afinal abstract
class would be a logical contradiction.If your class just have
static
methods, maybe you should justhide
its constructor, by defining it asprivate
.-It is not possible because the Java language specification states that:
Other than this, there is no inherent reason why an abstract final class would be illogical - the word abstract is generally used in contrast to concrete to mean that no direct instances of a type may exist.
This is the same reason why abstract methods cannot have access modifier private.
A method can never, ever, ever be marked as both abstract and final, or both abstract and private. Think about it—abstract methods must be implemented (which essentially means overridden by a subclass) whereas final and private methods cannot ever be overridden by a subclass. Or to phrase it another way, an abstract designation means the superclass doesn't know anything about how the subclasses should behave in that method, whereas a final designation means the superclass knows everything about how all subclasses (however far down the inheritance tree they may be) should behave in that method. The abstract and final modifiers are virtually opposites. Because private methods cannot even be seen by a subclass (let alone inherited), they too cannot be overridden, so they too cannot be marked abstract.
There is no reason except subjectivity but you can accomplish the same objective by making your class abstract and make all your methods final. Like:
The compiler will check this and give an error if you try to instantiate an object of MyClass, and it will also not be possible to override the final methods when any subclass extends MyClass