Static method overriding | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Static method overriding

I have a code with a static method Print(String output){} Print("sth"); are all around Now I want in the exact moment to override the method so that Print("sth"); now does the new task. But if the condition doesnt satisfy the result the method will keep doing the old job. Is there any ways apart from denying the idea of a static method?

27th Apr 2017, 8:03 PM
Alex Snaidars
Alex Snaidars - avatar
2 Answers
+ 10
You could overload your static method or extend the class that contains the static method without overriding it and hide your 'new' static method in the child class. For the concept of hiding see http://stackoverflow.com/questions/2475259/can-i-override-and-overload-static-methods-in-java
27th Apr 2017, 11:01 PM
Tashi N
Tashi N - avatar
+ 2
Java doesn't allow override static methods. Overriding it's a mechanism of polymorphism and depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclas will have different behaviors for the same methods defined in the superclass. A static method not associated with any instance of class so the concept is not applicable.
27th Apr 2017, 9:28 PM
Alexey
Alexey - avatar