Search code examples
javadeprecation-warning

Is it a bad practice to override deprecated methods without actually changing what they return?


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();
    }

Solution

  • 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.