Sunday, 23 July 2017

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

We cannot make an abstract class final in Java.

No, it's not possible to have an abstract method in a final class in Java. Why?
Because whenever you will 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 hence it's not possible to have an abstract method in a final class in Java.

For the below code, the final class cannot be extended i.e. we cannot provide any implementation of the abstract method() in the subclass so there is no single reason to allow the abstract method in the final class which cannot be used.

package com.java.oops;
public final class AbsInFinal {

     public abstract void method() {
          
     }
}

For above piece of code, you will get below given error in Eclipse IDE:
The type AbsInFinal must be an abstract class to define abstract method

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...