0

Why the answer is changed after inserting static keyword?

class Base { public static void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public static void show() { System.out.println("Derived::show() called"); } } public class Main { public static void main(String[] args) { Base b = new Derived(); b.show(); } } Why here the answer is of super class But class Base { public void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } } public class Main { public static void main(String[] args) { Base b = new Derived(); b.show(); } } After overriding ans will be of sub class

1st May 2021, 11:31 AM
kreddyt
kreddyt - avatar
3 Respostas
+ 2
Static keyword will print the static functions of that class only (here base)
1st May 2021, 12:30 PM
Atul [Inactive]
+ 2
And in 2nd program you are implementing dynamic method dispatch
1st May 2021, 12:31 PM
Atul [Inactive]
0
Ans Same as cpp in java the overriding doesn't occur in the case of static
1st May 2021, 11:43 AM
kreddyt
kreddyt - avatar