Overload Or Override Static Methods in Java

If I talk about Overload Or Override a Static Methods in java, we need to remember few points.

Points about  static method to remember :-

  1. Static Method Belongs to class not Object.
  2. A static method can be accessed directly by the class name and doesn’t need any object

Can We OverLoad Static Methods In Java:-

The Answer is “Yes”. If we talk about Overloading methods the concept is same in case of static methods as well. In order to overload static, we need to provide another static method with same name but different Signature. Let’s have a look at One example.

TestOverload.Java

package test.main;

public class TestOverload {

	public static void main(String args[]) {

		TestOverload.show();
		TestOverload.show("FrugalisMinds");
	}

	public static void show() {
		System.out.println("Show Called ");
	}

	public static void show(String name) {
		System.out.println("Overloaded Show Called" + name);
	}
}

Output:-

Show Called 
Overloaded Show CalledFrugalisMinds

Also Read:

  1. Could Not Load Or Find Main Class
  2. Upcasting and DownCasting In Java

Whereas if we remove static keyword from one of the method, this is not overloading.For Example, let’s look at following code.

package test.main;

public class TestOverload {

	public static void main(String args[]) {

		TestOverload.show();
		TestOverload.show("FrugalisMinds");
	}

	public static void show() {
		System.out.println("Show Called ");
	}

	public   void show(String name) {
		System.out.println("Overloaded Show Called" + name);
	}
}

Output:-

This Will throw Compilation issue.

Can we Override Static Methods in Java:-

Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass. A static method is not associated with any instance of a class, hence answer is “NO”.

As you ca see in the example we have One Static method in base and derived class .Hence We might think its method Overloading , but its is not .

package test.main;

public class TestOverload {

	public static void main(String args[]) {

		Base obj = new Derived();
          
	       obj.display();  
	       obj.print(); 
	}

}

class Base {

	public static void display() {
		System.out.println("Base Class Display Called");
	}

	public void print() {
		System.out.println("Base Class Print Called");
	}
}

class Derived extends Base {

	public static void display() {
		System.out.println("Derived Class Display Called");
	}

	public void print() {
		System.out.println("Derived Class Print Called");
	}
}

Output:-

Base Class Display Called
Derived Class Print Called

Once we call obj.display()  , its is Method Hiding . Subclass has defined a class method with the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass.Here the version of a method that is executed will NOT be determined by the object that is used to invoke it. In fact, it will be determined by the type of reference variable used to invoke the method. Interviewers may confuse you a lot on this topic if you are a beginner.

Related Posts: