Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

What are Final Classes in Java



The final modifier for finalizing the implementations of classes, methods, and variables.

The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class.

You cannot extend a final class. If you try it gives you a compile time error.

Example

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

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
   at newJavaExamples.Sub.main(Sub.java:9)
Updated on: 2019-07-30T22:30:20+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements