How to override method that uses raw types?

206 views Asked by At

I have the following legacy class that I can not change:

import java.util.logging.Logger;

public class SuperClass
{
   // ...

   public Logger getLogger(Class c) {
      // ...
   }
}

I want to override the getLogger method in my class. Is it possible to do it without raw type usage?

1

There are 1 answers

1
Jatin On

As mentioned in the comments, you cannot override. It also makes sense, because overriding simply means Whatever that can be done with super-class can also be done with sub-class. If SuperClass getLogger can take Class (of all kinds) then the sub-class should also allow that.

In this case, you could create bridge methods if that fits your purpose:

class SubClass<T> extends SuperClass {

    @Override
    public Logger getLogger(Class c) {
        return getLoggerT(c);
    }

    public Logger getLoggerT(Class<T> c) {
        return super.getLogger(c);
    }
}

And this class can be used through out.