2

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?

samabcde
  • 6,988
  • 2
  • 25
  • 41
SternK
  • 11,649
  • 22
  • 32
  • 46
  • Just out of curiosity: what would you rather use instead of `Class c`? `Class>`? – ernest_k Jan 18 '21 at 09:37
  • 2
    No, you're stuck with using raw generic. Which shouldn't be an issue, you just need to add `@SuppressWarnings("rawtypes")` next to `@Override`, so you don't get any compiler warnings. – Andreas Jan 18 '21 at 09:38
  • 1
    @ernest_k Yes, `Class>` would be acceptable, but I can not use it – SternK Jan 18 '21 at 09:40
  • @Andreas Yes, I know about it, just want to be sure there is no another cleaner alternative. – SternK Jan 18 '21 at 09:46
  • 1
    Sorry, if you're stuck with code that predates Java 5, i.e. Java code from 2004 or earlier, you just have to live with the quirks. – Andreas Jan 18 '21 at 09:53
  • Which Logger are you using? Log4j? – Dale Jan 18 '21 at 10:28
  • @Dale This is a third party library that uses `java.util.logging` but general part of my application uses Log4j and I need to provide an adapter here. – SternK Jan 18 '21 at 10:44
  • So should it use Log4j or ??? – Dale Jan 18 '21 at 10:58
  • @Dale I use `java.util.logging.Logger` in this method, I have updated the question, but I do not see how it can help with answer to the question. – SternK Jan 18 '21 at 13:37
  • 1
    One option would be to update your dependencies. Get rid of that version of `SuperClass`, and get a newer one instead. – MC Emperor Jan 21 '21 at 10:28

1 Answers1

1

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.

Jatin
  • 31,116
  • 15
  • 98
  • 163
  • I can not understand why we can not use `Class> c` instead of `Class c`. Why this is not allowed? – SternK Jan 21 '21 at 09:29