inheritance in java does not work the way I expected -


i have following class:

public class x {     public void a() {         b();     }      private static void b() {         system.out.println("111111111");     } } 

now have following inherited class z:

public class z extends x {      private static void b() {         system.out.println("22222222");     } } 

now if do

z myclass = new z(); z.a(); 

i get: 111111111 result. (also, eclipse tells me b() in inherited class never called).

why? , how can run inherited b method?

the b methods static. when call method a uses implementation of class b (because that's method a defined). class b not aware of existence of class z , cannot call method of class z.

because method static, it's not overridden upon inheriting b. polymorphism works instances of class. static method not play polymorphism game , cannot overridden.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -