Introduction to Method Overriding in Java : Free Java Tutorials

Method overriding is as important as method overloading and a slightly tricky concept of Java. Whatever method Parent class contains, are available to the Child class through inheritance. If the child class is not satisfied with the parent class implementation, then the child is allowed to redefine that method based on its requirement. This process is called overriding.

The parent class method is called the overridden method and the child class method is called the overriding method.

Example of Method Overriding

// Parent class
class X
{
	// overridden method
	public void method()
	{
		System.out.println("This is overridden method");
	}
}

// Child class
class Y extends X
{
	// overriding method
	public void method()
	{
		System.out.println("This is overriding method");
	}
}

public class Demo
{
	public static void main(String[] args)
	{
		// parent class reference and parent class object
		X x = new X();
		
		// child class reference and child class object
		Y y = new Y();

		// parent class reference and child class object
		X obj = new Y();
		
		// calling method() function on each object
		x.method();
		y.method();
		obj.method();
	}
}

Output:

This is overridden method
This is overriding method
This is overriding method

To understand this output, first, understand the role of compiler and JVM in method overriding.

When we declared obj in the above example, it had a reference to parent class X and it is an object of child class Y.

The compiler will perform its duty just by checking that whether method() function is there in the parent class or not. Since class X has method() function, the compilation will be successful.

In overriding, method resolution is always taken care of by JVM based on Runtime object. Hence, overriding is also considered as runtime polymorphism, dynamic polymorphism or late binding.

So, at the runtime, JVM will check if child class Y has overridden the method of the parent class. If it is overridden, then JVM will execute child class method. Otherwise, it will execute parent class method.

Our child class Y has overridden method() function, so JVM will execute method() of the child class.

In overriding, method names and argument types must be matched i.e. method signatures must be identical.

Also, return types must be same in overriding. But, this rule is applicable until 1.4 version only.

From version 1.5 onwards, we can take co-variant return types according to which, child class method return type need not be identical to parent method return type but its child types are also allowed.

// Parent class
class X
{
	// overridden method
	public Object method()
	{
		return null;
	}
}

// Child class
class Y extends X
{
	// overriding method
	public String method()
	{
		return null;
	}
}

Above code will compile successfully on 1.5 onwards versions as String class is child class of Object class. However, this will not compile successfully on 1.4 or older versions.

// Parent class
class X
{
	// overridden method
	public double method()
	{
		return 10.00;
	}
}

// Child class
class Y extends X
{
	// overriding method
	public int method()
	{
		return 5;
	}
}

Note – co-variant return type concept is applicable only for object types and not for primitive types.

So, the above code will not compile successfully because double and int are primitive types.

Hope you find this introductory article on method overriding concept of Java helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *