7

Is this code bad practice as the method show() is deprecated? Is it okay to override here?

public class Window extends JFrame {

    public Window() {
        // Do things.
    }

    public void show() { // <- Comes up with a warning as deprecated code.
        // Do other things.
    }
}
akuzminykh
  • 4,522
  • 4
  • 15
  • 36
Parzavil
  • 388
  • 3
  • 13
  • Hey! I think this is kinda opinion-based and depends on the case. But it's an interesting question and I can't find a duplicate for it so far. – akuzminykh Jun 08 '20 at 14:53
  • 1
    If you're not calling the original method, the deprecation warning is not relevant - the deprecated functionality is no longer being used, unless this class employs aspects around this method. It does, however, generate compiler output static, which can be misleading unless you suppress the warning. Personally, I would write a new method. – Bucket Jun 08 '20 at 14:58
  • 5
    IMO the important thing to consider is, what will happen when the deprecated method is actually gone? – akuzminykh Jun 08 '20 at 15:01

1 Answers1

4

When it's a class that you're extending, it's ideally better to avoid overriding a deprecated method, as in a future release when/if that is removed, and you need to upgrade to the newer version of the library, you will have to rework with the removed method which was deprecated.

If in your instance this is the JFrame class that you're extending, and you intend to override the show() method, you can instead override the setVisible(boolean b) method (doc) which is the replacement for the show() method as mentioned in the javadoc.

Also, it is not advisable to override a base class method and entirely change its function, as

  • You cannot use the method for it's original use-case
  • The method's intent becomes misleading and makes no sense to override the method, when you can actually create a new method which clearly indicates its function
Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • 1
    The thing is I am not using show() in its original purpose, I am using it to change the components in a container, so surely if it is removed then it will just become like any other method? – Parzavil Jun 08 '20 at 15:14
  • 3
    @Parzavil overriding a method to completely change its intent is even worse – OrangeDog Jun 08 '20 at 15:23
  • Could I ask why though? Since my program must run on older machines I won't ever be updating my library beyond Java8. If it is more of a complex issue could you recommend any reading I could do into it, as I can not see any major issues with doing this? – Parzavil Jun 08 '20 at 15:24
  • 1
    @Parzavil are you asking me? https://en.wikipedia.org/wiki/Liskov_substitution_principle – OrangeDog Jun 08 '20 at 15:28
  • @Parzavil Premier support for Java 8 runs out in less than two years. Then what are you going to do? – OrangeDog Jun 08 '20 at 15:29
  • I have not ever used Premier support from oracle. Up until 2017 I used their binaries but since then I have moved entirely to the opensource binaries from AdoptOpenJDK – Parzavil Jun 08 '20 at 15:37
  • @Parzavil yes, and they will end support in less than two years – OrangeDog Jun 08 '20 at 15:50
  • @OrangeDog the end of support is a concern but I am just building something to be used for no more than a year, as their current system has been left to fall apart. However, I am trying to convince my employer that long term he needs to move off Windows XP. – Parzavil Jun 08 '20 at 16:04
  • Thank you for the edit @Madhu Bhat That answers my original question. – Parzavil Jun 08 '20 at 16:07
  • @Parzavil [everything after 2014-04 is unpatched](https://www.cvedetails.com/vulnerability-list.php?vendor_id=26&product_id=739&version_id=&page=1&hasexp=0&opdos=0&opec=0&opov=0&opcsrf=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opfileinc=0&opginf=0&cvssscoremin=0&cvssscoremax=0&year=0&month=0&cweid=0&order=1&trc=741&sha=96656e0273b52e8473fbf8b6371fe2ed4a0f8ae8) – OrangeDog Jun 08 '20 at 17:22