Java Tutorial/Class Definition/Final Class

Материал из Java эксперт
Перейти к: навигация, поиск

Final Classes

You can prevent others from extending your class by making it final using the keyword final in the class declaration.



final class FinalClass{

}





Using final to Prevent Overriding

class A {
  final void meth() {
    System.out.println("This is a final method.");
  }
}
   
class B extends A {
  void meth() { // ERROR! Can"t override.
    System.out.println("Illegal!");
  }
}