0

My class extends java.sql.Date because I need it to work easily and well with my database. However, java.sql.Date also extends java.util.Date which has deprecated methods such as getYear(), getMonth(), and getDay().

I don't like the idea that my team will have deprecated warnings pop up whenever they use the deprecated methods that I inherited in my class. So, should I override those deprecated methods just to trick the IDE into thinking that we're not using deprecated methods when using my class?

For example:

    @Override
    public int getYear() {
        return super.getYear();
    }
    
    @Override
    public int getMonth() {
        return super.getMonth();
    }
    
    @Override
    public int getDay() {
        return super.getDay();
    }
op_
  • 92
  • 7
  • You are still calling the deprecated method you will get warning on your class. – seenukarthi Oct 25 '21 at 08:28
  • Also, deprecated methods might be removed some time in the future and then your code will break. – QBrute Oct 25 '21 at 08:33
  • @KarthikeyanVaithilingam It only shows on my class, but it doesn't show on others which is what I'm aiming for. Just to avoid my team from having a bunch of deprecated warnings in their code when they use my class. – Carl Eric Doromal Oct 25 '21 at 08:35
  • @CarlEricDoromal you shouldn't override those methods, you shouldn't use those methods. Use the ones that replaced them. There is a reason they are deprecated – Stultuske Oct 25 '21 at 08:37
  • @QBrute I honestly don't think I have a choice since `java.sql.Date` is (afaik) the only thing there is that works well with SQL Date, but the tradeoff is that it also inherits from `java.util.Date` which is mostly deprecated. – Carl Eric Doromal Oct 25 '21 at 08:39
  • @CarlEricDoromal It's gives your team a false sense of not using a deprecated method but it's not the case they are calling a method which itself calling a deprecated method. – seenukarthi Oct 25 '21 at 08:39
  • 5
    *My class extends java.sql.Date because I need it to work easily and well with my database.* - This is a very bad choice. The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API. `java.sql.Date` inherits all those problems by extending `java.util.Date`. Check [this answer](https://stackoverflow.com/a/67752047/10819573) and [this answer](https://stackoverflow.com/a/67505173/10819573) to learn how to use `java.time` API with JDBC. – Arvind Kumar Avinash Oct 25 '21 at 08:40
  • @Stultuske agreed, but in this particular case the advice is to replace with Calendar.get() and that might be incompatible with *having* to use java.sql.Date for some reason or other ... – Erwin Smout Oct 25 '21 at 08:40
  • 2
    @ErwinSmout: `Calendar` is also part of the "old and busted" `java.util` group of date/time handling classes, it's definitely not the correct alternative. `LocalDate` is the most likely correct replacement for `java.sql.Date`. And why do you **have** to use ` java.sql.Date` anywhere except at the very point where you interact with the JDBC driver? Or does *all* of your code interact directly with JDBC (which would be a code smell, IMO). – Generous Badger Oct 25 '21 at 08:48

2 Answers2

3

No, don't do this. There is literally no reason whatsoever to do this.

Use @SuppressWarnings("deprecation") if you have made sure that you really, really just want to suppress the warning without actually doing anything about it.

But there is a reason why these methods are deprecated. Java has a new Date & Time API (java.time), which works much better. Most tools now support the new API. If at all possible, you should think about migrating to the new API.

Polygnome
  • 7,639
  • 2
  • 37
  • 57
0

There is usually a reason why methods get deprecated. In java's own packages, those reasons are documented extremely carefully. And very often, it is a bad idea to plain ignore that reason and think too lightly it doesn't apply to you.

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52