Method Overriding with respect to static methods in Java : Free Java Tutorials

In this tutorial, we will explore method overriding concept with respect to static methods. We will also see the difference between method hiding and method overloading with respect to static methods. In the end, we will find out whether method overriding is possible with the static methods or not.

We cannot override a static method as a non-static method. Otherwise, we will get a compile-time error.

Static methods are Class-level methods and non-static methods are object level methods. So JVM gives a compile-time error when we override a static method with a non-static one.

// Parent class
class Parent
{
	// parent class static method 
	public static void method() 
	{
	}
}

//Child class
class Child extends Parent
{
	// child class method 
	public void method() 
	{
	}
}

Here, method() in Child class cannot override method() in the Parent class since the overridden method is static.

Running the above code, we get a compile-time error saying that This instance method cannot override the static method from Parent.

Now, let’s change parent method to be non-static and child method to be static.

// Parent class
class Parent
{
	// parent class method
	public void method() 
	{
	}
}

//Child class
class Child extends Parent
{
	// child class static method  
	public static void method() 
	{
	}
}

We also cannot override a non-static method as static. Running above code will also give us a compile-time error since the overriding method is static.

If both parent and child class methods are static, then we will not get any compile time error. So, it seems that overriding concept is applicable to static methods.

But actually, it is method hiding and not overriding. Look at the example below.

// Parent class
class Parent
{
	// parent class static method
	public static void method() 
	{	
	}
}

//Child class
class Child extends Parent
{
	// child class static method  
	public static void method() 
	{		 
	}
}

Method Hiding vs Method Overriding

All rules of method hiding are exactly same as overriding except the following differences:

  1. The static methods cannot be overridden. They are always used for method overloading. (method hiding)
  2. In overloading, method resolution is taken care of by compiler based on reference type. Whereas, in overriding method resolution is always taken care of by JVM based on runtime object.
  3. Method hiding is also called compile-time polymorphism, static polymorphism or early binding. On the other hand, method overriding is also called runtime polymorphism, dynamic polymorphism or late binding.

Take a look at the following example.

// Parent class
class Parent
{
	// parent class static method
	public static void method() 
	{	
		System.out.println("Parent is executing");
	}
}

//Child class
class Child extends Parent
{
	// child class static method  
	public static void method() 
	{		 
		System.out.println("Child is executing");
	}
}

public class Demo
{
	public static void main(String[] args)
	{
		// Parent class reference and parent class object
		Parent p = new Parent();

		// Child class reference and child class object
		Child c = new Child();
		
		// Parent class reference and child class object
		Parent obj = new Child();
		
		// calling static method() on all 3 created objects
		p.method();
		c.method();
		obj.method();
	}
}

Output:

Parent is executing
Child is executing
Parent is executing

In this example, since both overridden and overriding methods are static, there is actually method overloading that will take place.

First two lines of the output are self-explanatory since they are called by objects which have same reference types and object types.

So, when we call the static method() using obj which has parent class reference and child class object, the compiler will do method resolution based on reference type and it will execute static method of the parent class.

This is all about Method Overriding with respect to static methods in Java. Hope you find this tutorial helpful.

Leave a Comment

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