I have the following example:
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
A<ConcreteErrorHandler> a = new A<ConcreteErrorHandler>();
a.m(); //Exception here!
}
public static class AbstractErrorHandler {
public static void handle(){
throw new UnsupportedOperationException("Not implemented");
}
}
public static class ConcreteErrorHandler extends AbstractErrorHandler{
public static void handle(){
System.out.println("Concrete handler");
}
}
public static class A<T extends AbstractErrorHandler>{
public void m(){
T.handle();
}
}
}
IDEONE
Why the method of the base class is called, but not of the derived? The signatures of the handle()
methods are perfectly the same. I know that static methods don't inherit, but shouldn't a compile-time error be thrown in my case then?
Could someone explain that behavior?
A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.
A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.
A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members. Static method uses complie time binding or early binding. Non-static method uses run time binding or dynamic binding.
Yes, derived classes have immediate access to objects and methods in the Base Classes they derive from ... if ... the Base Class exposes those objects and methods by the use of modifiers, like 'public.
The reason for this is that the compiler doesn't know which exact subtype of AbstractErrorHandler
will be replacing T
at Runtime. That's why it just binds the method call T.handle()
to the AbstractErrorHandler.handle()
method.
The problem here is that you're mixing inheritance with the static
features of the classes in Java.
In order to have this working (correctly), you have to get rid of the static
modifier for the .handle()
methods and keep an instance of T
in the A
class. This T
instance (at Runtime) will be some specific subclass of AbstractErrorHandler
and then the actual .handle()
method will be executed.
For example:
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
A<ConcreteErrorHandler> a = new A<ConcreteErrorHandler>(new ConcreteErrorHandler());
a.m();
}
public static class AbstractErrorHandler {
public void handle() {
throw new UnsupportedOperationException("Not implemented");
}
}
public static class ConcreteErrorHandler extends AbstractErrorHandler {
public void handle() {
System.out.println("Concrete handler");
}
}
public static class A<T extends AbstractErrorHandler> {
T instance;
A(T instance) {
this.instance = instance;
}
public void m() {
instance.handle();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With