Final, Finally (Part 3: Final Method)

welcome to part 3 of Final Keyword …

Final Method

We can declare a method as final. A final method cannot be overridden by a child class.

public class Dhaka {
    public final void uCanNotOverrideMe (){
        System.out.println("wow!");
    }   
}

public class Khulna extends Dhaka {
     public final void uCanNotOverrideMe (){
        System.out.println("wow!");  
     }
}

// Compile error: uCanNotOverrideMe() in Khulna cannot override uCanNotOverrideMe() in Dhaka; overridden method is final

If you don’t want a child class to override a method for some of the requirements, then you can use final methods.

enjoy!

Advertisements