Can we override the static method in java

The most common and tricky question in java, can we override static method? Before moving further I will recommend you please read the method overriding and static method in java. In this post, we will discuss can we override static method in java.
No, we can’t override the static method in java. If we declare a method with the same signature in child class. It considered the method hiding instead of method overriding.

Most beginners have a common doubt whether is it possible to override static method in Java? How it will work if java override static method? In this article, we will resolve all the ambiguities.

Before move further we should know about the method overriding.
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 with the same signatures. 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 static method?

In Java, we can declare a static method in the child class with the same signatures as in Parent class. But it will not be considered the method overriding. The static method of child class will hide the implementation of the method of Parent class. It is known as method hiding. Because the compiler decides which method to execute at the compile-time, not at the runtime. As you know the static method can be accessed by class name rather than the object. When the compiler invokes the static method then it accesses the static method of the child class.

can we override static method
class ParentClass 
{	
	public static void printData()
	{
		System.out.println("Method of Parent class");
	}
}

public class ChildClass extends ParentClass
{
	public static void printData()
	{
		System.out.println("Method  of Child class");
	}
	
	public static void main(String arg[])
	{
		// Here we are creating object of Parent class but static method always use class
		// So it will call the method of Parent class
		ParentClass parent = new ParentClass();
		parent.printData();
		
		// Here we are creating object of Child class but static method always use class
		// So it will call the method of Child class
		ChildClass child = new ChildClass();
		child.printData();
		
		// Here we are creating object of Child class and reference of Parent class
		// But static method always use the class name
		// So it will call the method of Parent class
		ParentClass childObject = new ChildClass();
		childObject.printData();
	}
}

Output: Method of Parent class
Method  of Child class
Method of Parent class

Leave a Comment