Can you make an Abstract Class or Method Final in Java? Example

No, you cannot make an abstract class or method final in Java because the abstract and final are mutually exclusive concepts. 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 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 the 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 modifiers with a class is illegal in Java. 

This is also one of the most popular Java interview questions, if you are preparing for Java interviews, knowing this concept can help you do well on your interviews. 

The 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 common sense once you know the concept in depth.

Btw, if you are new to the Java world or self-learning Java then I suggest you 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 an Abstract class be Final in Java?

There is a proverb in Hindi, "Haath Kangan ko Aarshi Kya, Padhe Likhe ko Parsi 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 that 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 the above Java source file using Java compiler (javac) from the command line you will receive a similar error. Eclipse uses a slightly different compile but it follows almost all rules of the 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 the 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 keywords 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.


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)
  • Review these 50 Java interview questions before the Interview (questions)
  • Can you override a static method in Java? (answer)
  • 10 Courses to Crack any Java Programming interview (best courses)
  • Can you overload a static method in Java? (answer)
  • 10 Data Structure and algorithms courses for coding interviews (online courses)
  • Is it possible to create an Abstract method in the final class? (answer)
  • Can you override a private method in Java? (answer)
  • Top 5 Websites to learn Java Coding for FREE (best websites)
  • 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 Structure 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 it with your friends and colleagues. If you have any questions 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 these free Core Java courses on Udemy. It's completely free, all you need is to create an Udemy account to access this course.

And, now over to you, Can you override a static or private method in Java? What about final method? can you override a final method in Java? Both of these are very popular Java interview question and you should know about it, if you don't just ask in comments and I will explain. 

7 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
  3. Superb explanation Javin. Thanks for the post

    ReplyDelete
  4. superb explanation ....it is too good to understand easily even who don't know the java ...Nice explanation....thanks you so much for posting this article..

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.