Saturday, April 15, 2023

Can you declare Abstract Class or Method Final in Java? If not, why?

If you are wondering whether you can use both abstract and final modifier together with a class or method in Java then you are not alone. This is one of the frequently asked, theory based Java questions which is sometime tricky to answer, particularly if you don't have complete knowledge of Java fundamentals. The answer of this question is No, you can not declare abstract class or method final in Java because both abstract and final keywords are mutual exclusive. Anything which is abstract cannot be final and vice-versa. It's like a person cannot be living and dead at the same time.

In order to understand this mutual exclusiveness, you need to know what does abstract and final means in Java. Once you know that concept you can reason about the answer your self and that's what you will learn in this article. 

In the past, I have shared many such thought provoking questions like can you overload a static method in Java or can you override main() method in Java, if you are new to Java then you can rea those article to get yourself familiar with subtle core Java details which only experts know. 



What is abstract modifier in Java?

The abstract keyword in Java is used to represent in-complete things in Java. For example, an abstract class is considered in-complete and it can only be used i.e. by creating object once you have extended the class and overridden its abstract method to mark the class complete. 

Also, abstract keyword can be applied to a class or method but not on a variable because it can't be incomplete. A method is said to be be abstract if you only declare the method without defining it. 

Similarly a class is said to be abstract, if it contains an abstract method of you just mark it abstract using keyword in the class declaration.  

An abstract class cannot be instantiated in Java but it can have constructor as we have discussed here. 

Here is some examples of abstract class and abstract method in Java:

public abstract class AbstractClass{

     public abstract void abstractMethod();

}

You can see that we have used abstract keyword in the class declaration which is mandatory because there is an abstract method in the class which automatically makes the class abstract. 



What is final modifier in Java?

The final keyword in Java signifies a complete thing. For example, if a variable is final which means you cannot assign it a new value once created. 

Similarly, if a class is final then you cannot extend it and if a method is final then you cannot override it to change it function. 

The key point is that final variables, methods, and classes are complete and they cannot be changed once declared. 


Here is an example of final class, method, and variable in Java:

public final class FinalClass{
 private final variable version = 3;

 public final void version(){
   return version;
 }

}

Once you made a class final, you cannot extend it which means you lost the power of inheritance





Why abstract class cannot be made final in Java?

Now that you know what does abstract and final means in Java, it's pretty easy to decide why we cannot make an abstract class final. Since abstract means in-complete and final means complete, it's logical that any class or method cannot be complete and in-complete at the same time. This means they cannot be abstract and final at the same time. 

They can be either in-complete or complete i.e. abstract or final but they cannot be both at the same time.   If you try to make an abstract class final in Java, the compiler will generate error as shown below:

package tool;


/**
* 
* A simple Java Program to demonstrate that a class
* cannot be both abstract and final at the same time. 
*/
public abstract final class Hello {

public static void main(String[] args) {
  // your awesome code

}

}

javac /tmp/iSrGFqg8oS/Hello.java
/tmp/iSrGFqg8oS/Hello.java:8: 
error: illegal combination of modifiers: abstract and final
public abstract final class Hello {
                      ^
1 error

You can see that using both abstract and final modifier together is an illegal combination in Java and you will always get a compilation error when you use these two keywords together, no matter whether you are using them with a class, a method or while declaring a variable in Java. 

Can we declare abstract class or method final in Java? If not, why?


That's all about whether you can declare an abstract class or method final in Java. No, you cannot. As we have seen, both abstract and final keyword are mutual exclusive and you cannot use them together. It will result in compile time error because abstract means in-complete and final means complete, and any class or method cannot be be in-complete and complete at the same time. They will be either incomplete or complete but cannot be both at the same time. 

Other Core Java Interview Questions you may like:

Thanks for reading this article so far. If you like this core Java interview question and my explanation then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

P.S. - If you are preparing for Java interview and need guidance then you can also checkout this list of best Java interview books and courses to prepare well for your next Java Interviews. In these articles you will find awesome resources which covers Java in its length and breadth and share common interview questions. 

No comments:

Post a Comment