0

I have written this java code

public interface Sorter {

  public static <T extends Comparable<T>> void sort(T[] array);

}

and the compiler throws this error at me:

Sorter.java:7: error: missing method body, or declare abstract
  public static <T extends Comparable<T>> void sort(T[] array);
                                               ^
1 error

By changing the semicolon to '{}' at the end of the method declaration, everything seems to be fine. But isn't this the way to define interfaces? What's the point of a body after all.

Theo Stefou
  • 389
  • 2
  • 16
  • 3
    Static interface methods are not abstract and must have an implementation – ernest_k May 24 '18 at 08:34
  • You have a static method. Static methods in interfaces (possible starting with Java8) need to be implemented in the interface itself as they can't be overriden. – Ben May 24 '18 at 08:35
  • @ErnestKiwele hey thanks. is there a specific reason for that? – Theo Stefou May 24 '18 at 08:35
  • oh, ok. thanks people – Theo Stefou May 24 '18 at 08:36
  • The reasoning is quite simple: A method needs to be abstract to be overriden. A static method is not abstract. So it can't be overriden. If you can't override it you can't create an implementation in a class implementing your interface. So you need to implement it in the interface itself. – Ben May 24 '18 at 08:37
  • @Ben So, would it be better to define something that would require an instance? My point for this code was to implement it in different ways (quicksort, bubble sort, merge sort) and not have to create an object every time I wanted to use the code (hence the static) – Theo Stefou May 24 '18 at 08:40
  • @TheoStefou imagine how you would call this method: `Sorter.sort()`. Now imagine you have implementations of this method in subclasses `QuickSorter`, `BubbleSorter` and `MergeSorter`. If you now just type `Sorter.sort()` what method is supposed to be called? – Ben May 24 '18 at 08:43
  • 2
    @Ben it's the other way round: a static method can't be overridden, so it can't be abstract. – Andy Turner May 24 '18 at 08:50
  • @AndyTurner thanks for the clarification! My error. – Ben May 24 '18 at 08:54
  • @Ben I was going to override it and call it like this Quicksorter.sort(); – Theo Stefou May 24 '18 at 09:01
  • @Ben But you guys made it clear why this doesn't work – Theo Stefou May 24 '18 at 09:01

2 Answers2

3

A static method is not overridable/implementable by the subclasses. It is static as the name conveys that.
So it would make no sense (and is so illegal at compile time) to define in an interface a static abstract method as no subclass could implement/override it.
Either define an implementation of the static method in your interface (possible since Java 8) or make it an instance method that subclasses could implement.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

you can't have a static method in an interface. Static methods are not instance dependent and hence can be executed straight from the class file.

More details can be found on

Why can't I declare static methods in an interface?

Though you can define a default method and a static method using Java 8

Ankuj
  • 713
  • 3
  • 10
  • 27