0

I don't usually post something this simple, but this issue kept me scratching my head for while.

I'm trying to override a method in a subclass, but I get the following error message:

enter image description here

My BitCompressor.java iextends Compressor.java and attempts to override its encodeInput(...) method, but when I compile, I get the following error: error: method does not override or implement a method from a supertype

The screenshot above shows the original method (middle), the subclass trying to override that method (top) and the error (bottom).

Any ideas? Thank you.

  • 7
    remove the `static` keyword. You cant override a `static` method – flakes Oct 27 '18 at 23:15
  • @flakes thanks! can you post your answer as an answer, so I can mark it as the solution? –  Oct 27 '18 at 23:18
  • 2
    Also please don't add images of code. [There are many reasons why you shouldn't.](https://meta.stackoverflow.com/a/285557/479156) – Ivar Oct 27 '18 at 23:22

1 Answers1

4

Remove the static keyword. You cant override a static method

To override a method it needs to be a normal instance method with visibility by the super class (ie public, protected or possibly package private) and not be marked final.

Every non-static method in Java is by default a virtual method except for final and private methods. These virtual methods are polymorphic and allow overriding.

flakes
  • 21,558
  • 8
  • 41
  • 88