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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s