Archive for Java

Java Final Method Example

Saturday, March 19th, 2011

A final method cannot be overridden by any subclass in java. This means if we try to override final method through subclass compiler will throw an error.

This is example of Java final method

 

FinalMethod.java


public class FinalMethod {
  public final void finalMethodClass()
  {
	  System.out.println("This is final method of java");
  }
}

Cannot override final methods

SubClassFinalMethod.java


public class SubClassFinalMethod extends FinalMethod{

	public void finalMethodClass()
	{
		// Error by compiler
		// Cannot override the final method from FinalMethod
	}
}

This will throw error as shown in code