Final Methods in Java


We can declare a method as final, once you declare a method final it cannot be overridden. So, you cannot modify a final method from a sub class. The main intention of making a method final would be that the content of the method should not be changed by any outsider.

Source Code

//Final Methods in Java
class Super
{
    public void display()
    {
        System.out.println("I am Super Display");
    }
    final void finalDisplay()
    {
        System.out.println("I am Super Final Display");
    }
}
class sub extends Super
{
    public void display()
    {
        System.out.println("I am Sub Display");
    }
}
 
public class finalMethods {
    public static void main(String args[])
    {
        sub o =new sub();
        o.display();
        o.finalDisplay();
    }
}
 

Output

I am Sub Display
I am Super Final Display
To download raw file Click Here

Basic Programs