+ 2

Can static method be overridden?

15th Aug 2016, 7:20 PM
WPimpong
WPimpong - avatar
3 Réponses
+ 2
It seems we can't. Because static method belongs to class not object. So when we call f.showName(), output is Father. Without static key word, method f.showName() will return Son because f is instance of Son. And this is called overriden.
15th Aug 2016, 9:08 PM
WPimpong
WPimpong - avatar
- 1
not possible y bec
15th Aug 2016, 7:41 PM
Madhu Mohan
Madhu Mohan - avatar
- 1
The example in below illustrates: class Father{ public static void showName(){ System.out.println("Father"); } } class Son extends Father{ public static void showName(){ System.out.println("Son"); } } public class Program { public static void main(String[] args) { Father f = new Son(); f.showName(); //Father Son s = new Son(); s.showName(); //Son } }
15th Aug 2016, 8:07 PM
Tiger
Tiger - avatar