Saturday, July 15, 2017

Is it possible to have an abstract method in a final class?

This is one of the interesting core Java questions which was asked to one of my readers recently during a telephonic interview for Java developer job interview. Even though he knows that you cannot make an abstract class final in Java, he got confused by the wording of the methods. The answer is simple, No, it's not possible to have an abstract method in a final class in Java. Why? because as soon as you declare an abstract method in a Java class, the class automatically becomes an abstract class and you cannot make an abstract class final in Java as discussed before, hence it's not possible to have an abstract method in a final class in Java.

Many Java programmers get confused in this question just because of the wording of the question, as I said, even the reader who was asked this question was familiar with the concept, he didn't realize that as soon as you declare an abstract method in a class, it will become an abstract class and it's not possible to make an abstract class final in Java.

This is the missing piece of information or you call it a trick which separates it from the more popular and frequently asked questions, can you declare a class both abstract and final in Java?

Let's see one code example to prove this point that you cannot declare an abstract method in a final class. We have a public final class called Hello which has an abstract method called print() in Hello.java file.

public final class Hello {

  public abstract print();

}

As soon as you type that in your Eclipse IDE, you will receive following error:
The type Hello must be an abstract class to define abstract methods



Here is another screenshot which shows that you cannot have an abstract method in a final class in Java:

Is it possible to have an abstract method in a final class?


Same is true when you write down this code in notepad and compile it using javac from the command-line window. As per Java specification, once you declare an abstract method inside a class, it automatically becomes an abstract class and since you cannot make an abstract class final in Java, the compiler will throw an error.

This is true for both top level or nested class in Java. Even if declare an abstract method on a nested final class you will receive the same error.

Another follow-up question on this topic is can an abstract class have static methods in Java? The answer is yes, there is no problem with declaring a static method inside an abstract class in Java because you don't need to instantiate a class to use the static method, you can just call them by using the class name.


We can modify our code example to include the main() method in the Hello class, which is a static method in Java as shown below:

public abstract class Hello {

  public abstract void print();

  public static void main(String args[]) {
     .. some code here
  }

}

You can see that there is no compile time error. The code compiles fine, hence it is perfectly ok to declare a static method inside an abstract class in Java.


That's all about whether it's possible to have an abstract method in a final class or not? As I said it's not possible because once you create an abstract method inside a class, as per Java specification the class automatically becomes an abstract class. Since you cannot make a final class abstract in Java, this becomes illegal and compiler prohibits you from doing that by throwing an error. But, yes, you can declare static methods on both final class as well as abstract classes, there is no problem with that. If you are learning Java, I suggest you further read Core Java for Impatient to get yourself familiar with core concepts like abstract and final class. It also covers Java 8.


Other related Java Interview Questions for practice
  1. Can abstract class have a constructor in Java? (answer)
  2. Can you override a static method in Java? (answer)
  3. Can you overload a static method in Java? (answer)
  4. Can you run a program without main() method in Java? (answer)
  5. Can you override a private method in Java? (answer)
  6. Can you overload and override the main() method in Java? (answer)
  7. Can you make array volatile in Java? (answer)
  8. Can you declare a class static in Java? (answer)

Further Reading
Java Fundamentals, Part 1 and 2
Java Programming Interview Exposed
Cracking the code interview - 189 qustions and solutions

Thanks for reading this article, if you like this core Java interview question and my explanation then please share with your friends and colleagues. 

No comments :

Post a Comment