Why are classes sometimes declared final in Java?


If a class is declared final, you cannot inherit it. If you try it gives you a compile-time error as −

Example

final class Super {
   private int data = 30;
}
public class Sub extends Sub {
   public static void main(String args[]){
   }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
   at Sub.main(Sub.java:7)

Updated on: 30-Jul-2019

495 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements