0

I have two classes and an interface. I created methods in the interface and overrode them in a class that implements the interface. Now I am trying to import these methods into another class. For some reason they methods are not found. Very frustrating because I do not know why. The interface, overriding class, and class that imports the methods are below in that exact order. Why can't they import?:

public interface SortInterface {

    public abstract void recursiveSort(int[] list);

    public abstract void iterativeSort(int[] list);

    int getCount();

    long getTime();

}

overriding class:

public class YourSort implements SortInterface{
      @Override
    public void iterativeSort(int[] list) {
        for(int i =1; i< list.length; i++){
        int temp = list[i];
        int j;
        for (j = i-1; j>=0 && temp < list[j]; j--)
            list[j+1] = list[j];
        list[j+1] = temp;   
    }}

    public static void recursiveSort(int array[], int n, int j) {
if (j < n) {
    int i;
    int temp = array[j];

    for (i=j; i > 0 && array[i-1] > temp; i--) array[i] = array[i-1];
    array[i] = temp;

    recursiveSort(array,n, j+1);  }} 


    @Override
    public void recursiveSort(int[] list) {
        int j = list.length;
         int n=0;
         if (j < n) {
        int i;
        int temp = list[j];

        for (i=j; i > 0 && list[i-1] > temp; i--) list[i] = list[i-1];
        list[i] = temp;

        recursiveSort(list,n, j+1);  }
    } 



    @Override
    public int getCount() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public long getTime() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

importing class:

public class SortMain {
 static int[] b;
static int[] c;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        b = new int[5];
        b[0]= 8;
        b[1]=5;
        b[2]=9;
        b[3]=4;
        b[4]=2;
        recursiveSort(b);

        c = new int[5];
        c[0]= 8;
        c[1]=5;
        c[2]=9;
        c[3]=4;
        c[4]=2;
        recursiveSort(b);

        for(int i = 0; i<5; i++){
            System.out.println(b[i]);
        }

        iterativeSort(c);
         System.out.println("");

        for(int i = 0; i<5; i++){
            System.out.println(c[i]);
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
jillian
  • 52
  • 6
  • What do you think `recursiveSort(b);` should do? Why do you think? – Sotirios Delimanolis Nov 16 '15 at 23:29
  • recursiveSort(b) is a method that implements a recursive sort. – jillian Nov 16 '15 at 23:30
  • No, that's not what I mean. What do you think the _expression_ should do and why do you think so? Is it a method invocation? A static or instance method? If it's a static method, what class does it belong to? How does the compiler know? If it's an instance method, which instance? How does the compiler know? – Sotirios Delimanolis Nov 16 '15 at 23:31
  • As this is appears to be homework and there is no obvious reason for using an interface I assume you have been told to use one. If not, you should have a static method on a utility class (see my answer) – Peter Lawrey Nov 16 '15 at 23:34
  • I was told to use an interface. The class YourSort, posted above implements the class. YourSort also implements the interface for that method. – jillian Nov 16 '15 at 23:35

1 Answers1

0

All methods on an interface are public abstract implicitly. Until Java 8 you could do anything else with a method.

All methods on an interface (before Java 8) were instance methods as you have written them. This means they must work on an instance of the interface.

Note: the way you have written the methods, they don't actually need the instance and probably should be on an interface at all. You could make them static methods, but a better alternative is to move them to a utility class and import static them there from SortMain.

For a minimum of changes you can write

new YourSort().recursiveSort(b);

While this will work it is horrible for a number of reasons, not least because the YourSort object is pointless and only required to get the code to compile.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130