In this post we will discuss about one of very tricky core java interview question and question is that Can we override static method in Java?
This is the trickiest Java interview question.
Static methods cannot be overridden in the exact sense of the word, but they can hide parent static methods.
Parent class static methods are not part of a child class (although they are accessible), so there is no question of overriding it. Even if you add another static method in a subclass, identical or with same method signature to the one in its parent class, this subclass static method is unique and different from the static method in its parent class.
This Java Program will show that, we can not override static method in Java. If you declare same method in subclass then It is known as method hiding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials.client; public class Animal { public void eat(){ System.out.println("Animal is eating.."); } /** * Static method in super class which will be hidden in * child class */ public static void run(){ System.out.println("Animal is running.."); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kkjavatutorials.client; public class Dog extends Animal{ public void eat(){ System.out.println("Dog is eating.."); } /** * Static method with the same method signature as in super class * Since static method can't be overridden so it is called method hiding. */ public static void run(){ System.out.println("Dog is running.."); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.kkjavatutorials.client; /** * @author KK JavaTutorials *Can we override static method in Java? */ public class ClientTest { public static void main(String[] args) { Animal animal = new Dog(); /* * As refrence aninal has child class Object so * Overridden eat method will be called.*/ animal.eat(); /*It will call parent class static method * Don't forget that static method can't be overridden * */ animal.run(); /*As static method can not be Overridden so Child class method be be called and it will hide parent class static method*/ Dog.run(); } } |
Output of above program:
1 2 3 |
Dog is eating.. Animal is running.. Dog is running.. |
That’s all about Can we override the static method in Java?
You May Also Like:
Can we call static method with null object?
Why should I choose Java for Software Development? What are Pros and Cons of Java?
Can we cast an int value into byte type variable? what if the value of int is larger than byte?
Can We store a double value in a long type variable without casting?
How many ways we can create an object in Java?
Why Java is not 100% Object-oriented language?
What are the differences between 32-bit and 64-bit versions of Java?
What will be the output of following java program?
What is the difference Between java.util.Date and java.sql.Date in Java ?
What is difference between using Serializable & Externalizable Interfaces in Java?
Which Java data type would you choose for storing sensitive information, like passwords, and Why?
What is left shift << and Unsigned right shift >>> operator in Java? How are these useful?
If you have any feedback or suggestion please feel free to drop in below comment box.