+ 2
Can static method be overridden?
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.
- 1
not possible y bec
- 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
}
}