简体   繁体   中英

Why can't a Java class be both abstract and final

Suppose I've a utility class which contains only static methods and variables. eg:

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?

It is not possible because the Java language specification states that:

It is a compile-time error to declare an abstract class type such that it is not possible to create a subclass that implements all of its abstract methods [1]

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 final class can't be extended, an abstract class needs to be extended in order to be instantiated. Therefore, a final abstract class would be a logical contradiction.

If your class just have static methods, maybe you should just hide its constructor, by defining it as private .-

private StringUtils() {
}

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:

public abstract class MyClass {
    …
    public final static void myMethod1() {
        …
    }

    public final static void myMethod2() {
        …
    }
}

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

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

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..

When we declare a class as an abstract it cannot be instantiated . When we declare a class as final it cannot be inherited. To implement the methods in the abstract the Class need to be inherited. But if we declare a class as final, the class cannot be inherited . So, we cannot declare a class as both abstract and final

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM