Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling base and derived static methods of a type variable

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?

like image 861
St.Antario Avatar asked May 13 '15 12:05

St.Antario


People also ask

How do you call a static method in base class?

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.

How do you call a static method?

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.

What is the difference between calling static methods and non-static methods?

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.

Can we call derived class method from base class in Java?

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.


1 Answers

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();
        }
    }
}
like image 83
Konstantin Yovkov Avatar answered Nov 02 '22 20:11

Konstantin Yovkov