Final, Finally (Part 2: Final Class)

welcome to part 2 of Final Keyword. Lets Start …

Final Class

We can declare a class as final. After that we cant create child class under that final class.

example time.


public final class King {
    public static void main (String args []){
        System.out.println("king Arthur");   
    }
}

public class Test extends King {
    
}

// compile error: cannot inherit from final King

so why on earth you want a final class,

1. Final classes (Immutable) are read-only.
2. Thread safe, no synchronization overhead.

a very good explanation

Enjoy 🙂

Advertisements