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

23,227

Solution 1

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() {
}

Solution 2

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.

Solution 3

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

Share:
23,227
PC.
Author by

PC.

Software Engineer. Languages used: Java, C++, C#, .Net Experienced in Enterprise Web App and Mobile App Development. A part of Firefox development community.

Updated on October 26, 2021

Comments

  • PC.
    PC. over 2 years

    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?

  • Bart Friederichs
    Bart Friederichs over 10 years
    abstract doesn't need to be inherited. It is just not possible to instantiate it.
  • PC.
    PC. over 10 years
    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.
  • ssantos
    ssantos over 10 years
    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.
  • Praba
    Praba over 10 years
    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.
  • Deduplicator
    Deduplicator over 8 years
    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.
  • White_King
    White_King over 5 years
    the question is referring to a final abstract CLASS not a method