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.
*
* 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 class CanWeOverrideStaticMethod {
public static void main(String args[]) {
Screen scrn = new ColorScreen();
//if we can override the static method in Java then
* @author Javin Paul
*/
public class CanWeOverrideStaticMethod {
public static void main(String args[]) {
Screen scrn = new ColorScreen();
//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
//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 void show(){
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 void show(){
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
This explanation also clear this line-
ReplyDelete"Polymorphic method invocations apply only to overridden instance methods."
Javin Paul created a new blog on java..Coool.I love the way you write
ReplyDeleteis it possible to override main method in Java ? Since main method is static in Java, it looks we can not override main in Java but what will happen if I use varargs version of main method in Super class and String[] version of main method in SubClass, which main method will be invoked ?
ReplyDeleteFor your question "if I use varargs version of main method in Super class and String[] version of main method in SubClass, which main method will be invoked ? ",
DeleteWhen you take different arguments in your method, irrespective of whether it is main() or some other method,it is called method "overloading", not overriding.
good blog.........
ReplyDeletebut my program is overriding the static method. what wrong with it :-p
ReplyDeletestatic class SuperClass {
public static void Static(){
System.out.println("parent");
}
}
static class SubClass extends SuperClass{
public static void Static(){
System.out.println("child");
}
}
This program will not compile bcoz You can use static keyword only with ineer classes.
DeleteAbsolutely right! Outer class can never be static in Java.
Delete@ AnonymousDecember 15, 2013 at 8:24 AM :
Try the example that Javin explained but this time make the static methods as non static in both Screen and ColourScreen classes i.e. public void show(){.....}
Rest of the things keep as they are and then see the output. It will give you:
Overridden static method in Child Class in Java
If you compare this o/p with the program o/p that Javin explained then you will see that overriding is not done in first case but in the second... :)
Hope this helps,
RaHuL
superb it helped me a lot
ReplyDeletethanks for providing the notes
When I have tested this issue on JAVA 8. We are able to override static and private methods.
ReplyDeleteHi Unknown, can you please detail me what exactly did your do for overriding static method? Actually I wanted to override main is that possible in later versions of Java
Deletepublic class StaticMethodOverriding {
ReplyDelete/**
* @param args
*/
public static void main(String[] args) {
System.out.println(Toyota.enginePower());
}
}
class Car {
private int tyreSize = 20;
private static int carEnginerPower = 3200;
public int tyreSize() {
return tyreSize;
}
public static int enginePower() {
return carEnginerPower;
}
}
class Toyota extends Car {
private static int carEnginerPower = 4500;
public static int enginerPower() {
return carEnginerPower;
}
}
The result of this program is 3200 which means as mentioned above "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.
Read more: https://www.java67.com/2012/08/can-we-override-static-method-in-java.html#ixzz8MZJKOO7K" . It is slightly wrong as we can override the static method as and above Java8