Can We Override static Method in Java?

Can we override static method? is another important java interview question.

Lets first take a look on what is method overriding.

Method Overriding: When parent and child class have methods with same signature then it is called as method overriding. At run time compiler decides which method to call depending upon object type.

Also Read: Can We Overload main Method in Java?

Can We Override static Method in Java?

No, we can’t override static method.

Static method is not a part of object, it belongs to class. It doesn’t make any sense in overriding static method. Although if you do so it will not be considered as overriding. Take below example.

class Parent {
	static void function() {
		//some code
	}
}

class Child extends Parent {
	static void function() {
		//some code
	}
}

Class Parent and Child contains static method function() with same signature. But its not method overriding. It is called as hiding because static method in child class hides the static method of parent class. Here compiler decides which method to call at compile time.

Comment below if you have doubts or found any mistake in above tutorial.

Leave a Comment

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