java - Static methods inheritance and polymorphism -
how inheritance , polymorphism work static methods? explain proper output supposed here , how derived?
class { public static int get() { return 17; } } class b extends { public static int get() { return 42; } } main x = new b(); x.get(); the error message,
the static method get() type should accessed in static way
i think know how access in static way question class , sort of implied 1 or other values returned
in our program, have class definitions:
class { public static int get() { return 17; } } class b extends { public static int get() { return 42; } } and somewhere else declare a x = new b();
what number call x.get() return?
the invocation return int value 17.
static methods not inherited.
static methods bound , invoked based on type of expression invoked on.
so
x.get(); where x of type a invokes a's implementation. note static methods don't need instance invoked.
these work well.
((a) null).get(); a.get(); ... public static somemethod() {} ... somemethod().get(); the message got warning, not error.
Comments
Post a Comment