Hey everyone. This is pretty lame of me having to do this, but I haven't done Java for about a year and this algorithms class I'm taking assumes we remember all the details. Anyway, I'm trying to implement a method from an interface in another class's main method.

Here's the interface:

public interface SortingAlgorithm extends Algorithm
{
    public int[] createSortIndex(java.lang.Comparable[] data);
    
    public int[] createSortIndex(int[] data);
    
    public void sortInPlace(java.lang.Comparable[] data);
    
    public void sortInPlace(int[] data);
}

and here's the main class:

abstract public class InsertionSort implements SortingAlgorithm{

  static int rangeLow = 100;
  static int rangeHigh = 1000;

  static int[] makeRandomData (int numValues)
  {
    int[] data = new int [numValues];
    for (int i=0; i<data.length; i++)
      data[i] = (int) UniformRandom.uniform ( (int) rangeLow, (int) rangeHigh );
    return data;
  }

  static void printData (int[] data)
  {
    System.out.print ("Data: ");
    for (int i=0; i<data.length; i++)
      System.out.print (" " + data[i]);
    System.out.println ("");
  }
  

  static void sortInPlace (int[] data)
  {
    for(int i = 1; i < data.length; i++)
    {
        int index = data[i];
        int j = i;
        while((data[j - 1] > index) && (j > 0))
        {
            data[j] = data[j - 1];
            j = j - 1;
        }
        data[j] = index;
    }
  }

  public static void main (String[] argv) 
  {
    if ( (argv == null) || (argv.length == 0) ) {
      System.out.println ("Usage: java InsertionSort <number-of-values>");
      System.exit(0);
    }

    try {
      int numValues = Integer.parseInt (argv[0]);
      int[] data = makeRandomData (numValues);
      System.out.println ("BEFORE SORTING: ");  
      printData (data);
      sortInPlace (data);
      System.out.println ("AFTER SORTING: ");
      printData (data);
    }
    catch (Exception e) {
      System.out.println (e);
    }
  }

}

The issue is that I get a compilation error about the fact that I'm trying to override an interface's method with a static method. The problem is it also won't let me use a non-static method in the main() method. So basically, what do I have to do to make this method from an interface work in the main method?

I feel really silly for asking this question, but it's been a really long time. Thanks for your patience.

You will have to declare an instance method if you are overriding.

To use a non-static method in main(), you will need to create an instance/object of the class. Since this class is abstract, you would not be able to achieve it unless you implement a concrete subclass for this abstract class.

Member Avatar for jtonic
abstract public class InsertionSort implements 
SortingAlgorithm {
    //....
    public void sortInPlace(int[] data) {
        for (int i = 1; i < data.length; i++) {
            int index = data[i];
            int j = i;
            while ((data[j - 1] > index) && (j > 0)) {
                data[j] = data[j - 1];
                j = j - 1;
            }
            data[j] = index;
        }
    }
    //...
}
   public static void main(String[] argv) {
        if ((argv == null) || (argv.length == 0)) {
            System.out.println("Usage: java InsertionSort <number-of-values>");
            System.exit(0);
        }

        try {
            int numValues = Integer.parseInt(argv[0]);
            int[] data = makeRandomData(numValues);
            System.out.println("BEFORE SORTING: ");
            printData(data);
            InsertionSort insertionSort = new InsertionSort() {

                public int[] createSortIndex(Comparable[] data) {
                    return null;
                }

                public int[] createSortIndex(int[] data) {
                    return null;
                }

                public void sortInPlace(Comparable[] data) {
                }

            };
            insertionSort.sortInPlace(data);
            System.out.println("AFTER SORTING: ");
            printData(data);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.