Can You Make an Abstract Class or Method Final in Java?Hello guys, if you a Java developer wonderi
Hello guys, if you a Java developer wondering whether you can make an abstract method final in Java or an abstract class final in Java but don’t know the answer then you have come to the right place. This is one of the frequently asked core Java questions and answer is No.
You cannot make an abstract class final in Java because both abstract and final are mutually exclusive i.e. an abstract class is incomplete and only way to use it by extending the class and implementing the abstract method to make it a concrete class, but once you make the class final, you just cannot extend it, hence it is not possible to make a class both final and abstract in Java.
They are also completely opposite concept i.e. an abstract class logically represent something which is incomplete while a final class logically represent a complete class which should not be extended further.
Sometime, final is used to prevent someone from extending key classes for security purpose. For example, java.lang.String
has been made final in Java, so that no one can extend it and change its behavior.
Why? because it is used in several security sensitive class e.g. ClassLoader
uses class name given in String to load class and Network related classes uses hostname and IP address given in String format to make network connection.
If someone is able to gain access of String
and overrides the behavior so that you can change the value once created, you effectively compromise the security of system because you try to make connection to host A but you ended up connecting to host B.
This is also an important Java Interview question and often asked in Java developer interviews, knowing this concept well not only help in interviews but also in your day to day job of reading and writing Java code.
By the way, If you are preparing for Java interviews then you can also checkout my books Grokking the Java Interview, and Grokking the Spring Boot Interview, where I have shared tips, tricks and frequently asked Java questions from different topics. You can also use the code — friends20 to get a 20% discount now.
Grokking the Spring Boot Interview
SPECIAL OFFER 20% OFF (CODE — FREINDS20) — $15.92/ $19.90Crack your Java and Spring Developer interview by preparing…
gumroad.com
Coming back to the question, It’s also a compile time error to declare both abstract and final keyword together, this means not only an abstract final class is not allowed but also you cannot make an abstract method final in Java due to same reason.
An abstract method is used by overriding it on subclass but once you make a method final in Java, you just cannot override them, means you would never be able to override your abstract method.
Since this is completely opposite of the purpose of abstract method, Java doesn’t allow you to declare an abstract method final. It is again a compile time error to declare an abstract method final in Java.
Here is a code example to prove my point that an abstract class cannot be made final in Java.
// Abstract class with an abstract method abstract class Animal { // Abstract method abstract void makeSound(); } // Concrete class extending the abstract class class Dog extends Animal { // Implementing the abstract method void makeSound() { System.out.println("Woof! Woof!"); } // Attempting to mark the abstract method as final (this will cause a compilation error) final void makeSound() { System.out.println("Woof! Woof!"); } } public class Main { public static void main(String[] args) { Dog myDog = new Dog(); myDog.makeSound(); } }
In this example:
Animal
is an abstract class with an abstract methodmakeSound()
.Dog
is a concrete class that extendsAnimal
and provides an implementation formakeSound()
.- An attempt is made to mark the abstract method
makeSound()
in theDog
class as final, which will result in a compilation error.
Attempting to compile this code will produce an error message similar to:
error: makeSound() in Dog cannot override makeSound() in Animal
final void makeSound() {
^
overridden method is abstract
1 error
This error indicates that an abstract method cannot be marked as final. The combination of abstract
and final
is contradictory in Java, as abstract
suggests that the method needs to be implemented by the subclasses, while final
indicates that the method cannot be overridden.
In Java, an abstract method is implicitly abstract and cannot be marked as final. Attempting to do so will result in a compilation error.
Now, let’s some code example to prove our point that we cannot declare an abstract class final in Java and if anyone tries to do that, compiler will prevent it by throwing error.
// Abstract class abstract class Shape { // Abstract method abstract void draw(); } // Attempting to declare the abstract class as final (this will cause a compilation error) final abstract class Circle extends Shape { // Implementation of the abstract method void draw() { System.out.println("Drawing a circle"); } } public class Main { public static void main(String[] args) { Circle circle = new Circle(); circle.draw(); } }
In this example:
Shape
is an abstract class with an abstract methoddraw()
.- An attempt is made to declare the abstract class
Circle
as final, which is not allowed in Java.
If you try to compile this code, you will receive a compilation error similar to:
error: illegal combination of modifiers: abstract and final
final abstract class Circle extends Shape {
^
1 error
This error indicates that combining the final
and abstract
modifiers for a class is not allowed. A class marked as abstract
is expected to be subclassed, and marking it as final
contradicts this purpose, leading to a compilation error.
I mean, In Java, an abstract class is meant to be subclassed, and it cannot be marked as final. If you attempt to do so, you will encounter a compilation error as shown in above example.
It is one of the frequently asked core Java questions, particularly during telephonic interviews, just to check if candidate is familiar with concept of abstract and final modifier in Java or not.
Depending upon the response of the candidate, many follow-up questions can be asked e.g. what is the use of final modifier in Java? Why you make a class or method final in Java? What happens when you make a variable final in Java etc. You can find answers of such questions on my list of 21 final modifier interview questions.
That’s all about whether you can make an abstract class/method final in Java or not. As you seen, it’s not possible to declare an abstract class or abstract method final Java, it will result in compile time error.
The reason is simple, both abstract and final modifier are mutually exclusive, which means you can use only one of them at a time.
It is also a common sense because abstract represent an incomplete thing while final represent a complete thing, a class or method cannot be both incomplete and complete at the same time.
Thanks for reading this article, if you like this article then please share with your friends and colleagues.
P. S. — If you are preparing for Java interviews then you can also checkout my books Grokking the Java Interview, and Grokking the Spring Boot Interview, where I have shared tips, tricks and frequently asked Java questions from different topics. You can also use the code — friends20 to get a 20% discount now.