Can You Override static method in Java? Method Hiding Example (original) (raw)
Can we override the static method in Java?
This is one of the most popular Java interview questions. The answer to this question is No, you cannot override the static method in Java because the method overriding is based upon dynamic binding at runtime and static methods are bonded using static binding at compile time. This means static methods are resolved even before objects are created, that's why it's not possible to override static methods in Java. Though you can declare a method with the same name and method signature in the subclass which does look like you can override static methods in Java but in reality that is method hiding.
Method overriding is an object-oriented concept that is based upon method resolution at runtime depending upon which object is calling the method rather than which class variable is holding the reference.
Java won't resolve the static method call at runtime and depending upon the type of object which is used to call static methods, the corresponding method will be called.
It means if you use Parent class's type to call a static method, original static will be called from a patent class, on the other hand, if you use Child class's type to call static methods, the method from child class will be called.
In short, you can not override the static method in Java. If you use Java IDE like Eclipse or NetBeans, they will show a warning that the static method should be called using class name and not by using object because a static method can not be overridden in Java.
Overriding Static method in Java - Example
In the last section, you learn the theory that we can not override static methods in Java, the static method can only be hidden in sub-class. Also, don't get confused between overloading and overriding as you can overload a static method in Java but you cannot override it.
/**
*
* Java program which demonstrates that
* you can not override static methods in Java.
* Had Static method can be overridden,
* with Superclass type and subclass object
* static method from subclass would be called in our example,
* this is not the case.
*
* @author Javin Paul
*/
public classCanWeOverrideStaticMethod {
public static voidmain(String args[]) {
Screen scrn = newColorScreen();
//if we can override the static method in Java then
// this should call the method from Child class
//IDE like Eclipse or IDEA will show a warning, //the static method should be called from the class name
scrn.show();
}
}
class Screen{
/*
* public static method which can not be overridden in Java
*/
public static voidshow(){
System.out.printf("The static method from parent class");
}
}
class ColorScreen extends Screen{
/*
* static method of the same name and method signature
* as existed in super
* class, this is not method overriding instead this is called
* method hiding in Java
*/
public static voidshow(){
System.err.println("Overridden static method
in Child Class in Java");
}
}
Output:
The static method from the parent class
This output confirms that you can not override the static method in Java and the static method is bonded at compile-time, based upon the type or class information and not based upon actual Object which is created at runtime. That's why static methods are also called "class methods" in Java because they belong to the class and not object.
Had the static method been overridden, the method from the Child class of ColorScreen would have been called, which wasn't the case here.
That's all on discussion Can we override the static method in Java or not. We have confirmed that no, you can not override a static method, we can only hide the static method in Java. Creating a static method with the same name and method signature is called Method hiding in Java.
Other Java Articles and Resources you may like
- 10 Java Coding Interview Questions and Answers for Java beginners.
- 5 differences between Hashtable and HashMap in Java
- How to traverse iterate or loop ArrayList in Java
- What is Serialization in Java – Serializable interface?
- What is the class file in Java - How to create Class in Java
- Difference between overloading, overriding, hiding and shadowing in Java