Can a class in Java be both final and abstract?

Java 8Object Oriented ProgrammingProgramming

An abstract cannot be instantiated. Therefore to use an abstract class you need to create another class and extend the abstract class and use it.

If a class is final you can’t extend it further.

So, you cannot declare a class both final and abstract.

Example

Still if you try to do so you will get a compile time error saying “illegal combination of modifiers:”

final abstract class Demo{
   public final void display(){
      System.out.println("Hello welcome to tutorialspoint");
   }
}

Output

C:\Sample>javac Demo.java
Demo.java:1: error: illegal combination of modifiers: abstract and final
final abstract class Demo{
               ^
1 error
raja
Published on 29-Dec-2017 04:31:30
Advertisements