Why are classes sometimes declared final in Java?

Java 8Object Oriented ProgrammingProgramming

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)
raja
Published on 05-Jan-2018 12:56:53
Advertisements