Prevent a class from being extended

 

  • A class cannot be used as a superclass if it contains the final keyword in its class definition:

    
    /* ======================================
       This class cannot be extended
       ====================================== */
    public final class myClass 
    {
       // Data fields, constructors, and methods omitted
    }
      

Prevent a inherited method from being overridden

 

  • You can disallow a method to be overridden by the inheritance mechanism by using the final keyword:

    public  class myClass 
    {
        ....
    
        /* ======================================
           This method cannot be overridden
           ====================================== */
        public final void method1()
        {
            // Do something
        }
    
        ....
    }