Can you make an Abstract Class/Method Final in Java?

No, you cannot make an abstract class or method final in Java because the abstract and final are the mutual exclusive concept. An abstract class is incomplete and can only be instantiated by extending a concrete class and implementing all abstract methods, while a final class is considered as complete and cannot be extended further. This means when you make an abstract class final, it cannot be extended hence it cannot be used and that's why Java compiler throws a compile-time error when you try to make an abstract class final in Java. In short, an abstract class cannot be final in Java, using both abstract and final modifier with a class is illegal in Java.

Same is true for abstract methods, you cannot make the final in Java. An abstract method must be overridden to be useful and called but when you make the abstract method final it cannot be overridden in Java, hence there would be no way to use that method.

This is why making an abstract method final in Java will result in a compile-time error. In short, the use of keywords abstract and final together is illegal in Java. You can verify these facts by yourself by trying to make an abstract class/method final in Java. In this article, I'll show you an example of both to prove the above points.

Even though it may seem easy that both these concepts are mutually exclusive, many Java programmers get confused between them. They just don't understand that abstract means incomplete and final means complete, and how a thing can be both complete and incomplete at the same time? It's just a common sense once you know the concept in depth.

Btw, if you are new into Java world or self-learning Java then I suggest you to also join The Complete Java Masterclass on Udemy. One of the most comprehensive and up-to-date courses in Java. It is recently updated for Java 12 and I highly recommend to all my readers to learn Java or fill gaps in their learning.



Can we make an Abstract class final in Java?

There is a proverb in Hindi, "Haath Kangan ko Aarshi Kya, Padhe Likhe ko Pharsi kya", which means instead of saying just show. So, why not we instead of saying that whether we can make an abstract final in Java or not, just code it and see what happens.

Let's try that, the following is a sample code which uses both abstract and final modifier together at class declarations when you write this code in Eclipse, the IDE will suggest this is wrong by giving an error as shown below:

abstract final class ImAnAbstractClass{

}

Error: "The class ImAnAbstractClass can be either abstract or final, not both"

Unlike many others, this error is both clear and concise and shows that it's not possible to make a class both final and abstract at the same time in Java.

Btw, If you compile above Java source file using Java compiler (javac) from the command line you will receive a similar error. Eclipse uses slightly different compile but it follows almost all rules of Java compiler.

If you want to learn more about what rules Java compiler follows or more of such fundamental concepts, you can also join Java Fundamentals Part 1 and Part 2 courses on Pluralsight, two of the best courses to learn Java from scratch.

Can you make an abstract class/method final in Java?



Can we make an Abstract method final in Java

Now that, you know there is no way you can make an abstract class final in Java, let's try to make an abstract method final in Java. As I said above, this time also Java compiler should complain because both final and abstract are mutual exclusive keywords:

abstract class ImAbstract{

public final abstract void anAbstractMethod();
}

class ImConcrete extends ImAbstract{

@Override
public void anAbstractMethod() {
// TODO Auto-generated method stub

}

}

This will give the error "The abstract method anAbstractMethod in type ImAbstract can only set a visibility modifier, one of public or protected" when you write this code in Eclipse, as shown in the following screenshot:

Can an abstract class be final in Java



You will also receive the error "Cannot override the final method from an abstract" on the subclass, as shown in the following screenshot which shows cannot override the final method from an abstract class.

This makes it clear that it's not possible to make a class both abstract and final in Java. It's one of the fundamental concepts and  If you want to learn more about Java fundamentals, I suggest you read a good core Java book like Core Java For the Impatient by Cay. S. Horstmann.


Is it possible to make an abstract method final in Java


That's all about whether you can make an abstract class/method final in Java or not. The short answer is no, you cannot make the final and long answer is what I have explained in this post. Since abstract and final represent two mutually exclusive concepts they cannot be used together.

Some of you might be wondering about variables? Can we make a final variable abstract in Java? Well, a variable cannot be abstract in Java. The use of abstract keyword is illegal for a variable, it can only be used with class or method, hence there is a way you can make a final variable abstract in Java.


Further Learning
The Complete Java MasterClass
Java Fundamentals Part 1 and Part 2
Java Interview Guide: 200+ Interview Questions


Similar Java Interview Questions you may like to explore
  • Can abstract class have a constructor in Java? (answer)
  • Is it possible to instantiate an Abstract class in Java? (answer)
  • Can you override a static method in Java? (answer)
  • Can you overload a static method in Java? (answer)
  • Is it possible to create an Abstract method in final class? (answer)
  • Can you override a private method in Java? (answer)
  • Can you overload and override the main() method in Java? (answer)
  • Can you make an Array volatile in Java? (answer)
  • Can you declare a Class static in Java? (answer)
  • 50+ Data Strucgture and Algorithms Interview Questions? (questions)
  • Must Read Books for Java Programmers (books)
  • Some Free Online courses to learn Java 8 and 9 features? (courses)
  • 10 Frameworks Java Programmer Should Learn? (courses)
  • 5 Free Data Structure and Algorithms Courses for Beginners (courses)

Thanks for reading this article, If you like this interview question and my explanation then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

P. S. - If you are self-learning Java and looking for a project based free course to start your journey then you should check out the Practice Java by Building Projects course on Udemy. It's completely free, all you need is to create an Udemy account to access this course.

3 comments:

  1. "The use of abstract keyword is illegal for a variable, it can only be used with class or method, hence there is a way you can make a final variable abstract in Java."

    It seems that you made a typo: ...hence there is a way you can NOT...

    ReplyDelete
    Replies
    1. Yes, you are right. There is no way you can make a final variable abstract in Java. The "no" is missing. I'll update the article.

      Delete
  2. Awesome Post. Very clear and well written. Thumbs up.

    ReplyDelete