Can we override the final method in java

Most beginners have a common doubt can we override final method in java? In this article, we will resolve all the ambiguities and see how we can we override final method in java? If no, then what is the reason?

Method overriding in java is only possible with the concept of Inheritance. As you know by use of inheritance concept in Java, we can inherit the method of the parent class to child class. Method overriding in Java is a feature through which we can define the method in child class that is already defined in parent classes. If a method in a child class has the same name, and parameters as a method defined in the parent class, then the method is defined in the subclass is called overriding method and the method defined in the parent class is called an overridden method.

Whenever a child class extends a parent class, the child class automatically inherits all the methods defined in the parent class(Obviously private method doesn’t inherit). These methods are known as derived methods.

Can we override final method in java?

A method declared with the final keyword, then it is known as the final method. The final method can’t be overridden. A final method declared in the Parent class cannot be overridden by a child class. If we try to override the final method,  the compiler will throw an exception at compile time. Because if we are declaring any method with the final keyword, we are indicating the JVM to provide some special attention and make sure no one can override it. We will discuss the reason in detail.

class Student
{
	int rollNo;
	String className;
	String name;
	String fee;
	
	Student() 
	{}
	
	public Student(int rollNo, String className, String name, String fee) 
	{
		this.rollNo = rollNo;
		this.className = className;
		this.name = name;
		this.fee = fee;
	}
	
	public final void printData()
	{
		System.out.println("Name : " + name + ", RollNo: "+ rollNo + ", Class Name : "+ className);
		System.out.println("Fee details" + fee);
	}
	
	public void games()
	{
		System.out.println("Cricket, Hockey, Football");
	}
}

public class Record extends Student
{
	public final void printData()
	{
		System.out.println("Name : " + name + ", RollNo: "+ rollNo + ", Class Name : "+ className);
	}
	
	public void games()
	{
		System.out.println("Table tenis, Swimming");
	}
	
	public static void main(String arg[])
	{
		Student student = new Student(101, "MCA", "Ram", "10000");
		Record record = new Record();
		record.printData();
		record.games();
	}
}

Error: LinkageError occurred while loading main class create.Record java.lang.VerifyError: class create.Record overrides final method create.Student.printData()

can we override final method in java

Why we can’t override the final method?

As you know by use of the overriding concept, we can provide the different implementation in child classes. But sometimes we want to provide only default implementation to each child class. We can prevent the overriding of the method by use of the final keyword.  When we declare a method with the final keyword. It means we are preventing it so that any child class can’t override it. The final method indicates the JVM to apply theses restriction.
If any developer tries to override the final method the JVM shows the exception at compile time.

Leave a Comment