Why Classes are 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: 2019-07-30T22:30:20+05:30

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements