9

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?

PC.
  • 6,870
  • 5
  • 36
  • 71

7 Answers7

19

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.

Joe Lee-Moyet
  • 1,804
  • 1
  • 20
  • 24
  • Also: It is a compile-time error if a class is declared both final and abstract, because the implementation of such a class could never be completed (8.1.1.2) – Turkhan Badalov Jul 02 '23 at 20:57
13

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() {
}
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • 10
    `abstract` doesn't *need* to be inherited. It is just not possible to instantiate it. – Bart Friederichs Oct 01 '13 at 07:31
  • 8
    I know that is the basic idea of an object oriented language. But in my example there is no contradiction. I do not want to extend or instantiate my class. – PC. Oct 01 '13 at 07:32
  • 2
    Mmh as for me, abstract class makes sense when you add some abstract methods. If you don't want your class to be instantiated, you should consider defining its default constructor as private. – ssantos Oct 01 '13 at 07:34
  • Or if the sole need of having the class as abstract and final is so that nobody can override your implementation of a method, you could try marking that method alone as final. – Praba Oct 01 '13 at 11:16
  • 3
    Please elaborate why a class neither being instantiable (it's abstract) nor being extendable (it's final) is a contradiction. Java not allowing one to declare both with a keyword on the class doesn't make it so! It's still not that uncommon to hack around that by using a private constructor and discipline. – Deduplicator Oct 16 '15 at 23:20
1

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

White_King
  • 748
  • 7
  • 21
user1515520
  • 624
  • 1
  • 6
  • 10
0

Due to some certain reasons we can not use final keyword with abstract class.

  1. If we define abstract final then we can't extend it.
  2. Abstract class meant to be inherit by some other classes and provide the implementation of non-concrete methods.
  3. If we are defining abstract class as final the it will give the compile time error..
0

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

-1

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.

Sreedhar GS
  • 2,694
  • 1
  • 24
  • 26
-1

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

Ankur08
  • 11
  • 1
  • 3