0

ok. i have a standard mergesort class.

public class Merge extends SortAlgorithm {

public static void sort(Comparable[] a) {
    Comparable[] aux = new Comparable[a.length];
    sort(a, aux, 0, a.length -1);
}

protected static void sort(Comparable[] a, Comparable[] aux, int lo, int hi) {
    if(hi <= lo)
        return;
    int mid = (lo + hi)/2;
    sort(a, aux, lo, mid);
    sort(a, aux, mid+1, hi);
    if(less(a[mid+1], a[mid]))
        merge(a, aux, lo, mid, hi);
}

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] a = br.readLine().split(" ");
    sort(a);
    show(a);
}

protected static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {
    int i = lo , j = mid+1;
    for(int k = lo ; k <= hi ; k++)
        aux[k] = a[k];
    for( int k = lo ; k <= hi ; k++ ) {
        if(i > mid)                       //Boundary Conditions
            a[k] = aux[j++];              //
        else if(j > hi)                   //
            a[k] = aux[i++];              //
        else if(less(aux[i] , aux[j]))
            a[k] = aux[i++];
        else
            a[k] = aux[j++];
    }
}

}

I have extended this class to create a newer version of mergesort which uses less amount of auxiliary array. it has a different 'merge' and 'sort(Comparable[])' function

public class MergeSmallerAuxArray extends Merge {

public static void sort(Comparable[] a) {
    Comparable[] aux = new Comparable[a.length/2];
    sort(a, aux, 0, a.length - 1);
}

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] a = br.readLine().split(" ");
    sort(a);
    show(a);
}
//method overidden below
public static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {       int i = lo , j = mid+1;
    for(int k = lo ; k <= mid ; k++)
        aux[k] = a[k];
    for(int k = lo ; k <= hi ; k++) {
        if(i > mid)
            a[k] = a[j++];
        else if(j > hi)
            a[k] = aux[i++];
        else if(less(aux[i], a[j]))
            a[k] = aux[i++];
        else
            a[k] = a[j++];
    }
}

}

but each time i run this class i am getting an error because the sort(Comparable[],Comparable[],int,int) method of the superclass is calling the merge function of the superclass instead of the one which i had overridden. Where did i went wrong? Please Help. Thank you.

Bill F
  • 723
  • 11
  • 23
AribAlam
  • 27
  • 3
  • 2
    Possible duplicate of [Why doesn't Java allow overriding of static methods?](https://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods) – Bill F Jun 06 '17 at 20:00

1 Answers1

5

The issue here is that you use static keyword. static doesn't work with polymorphism.

Alex
  • 1,940
  • 2
  • 18
  • 36
  • Thanks a lot ! :) – AribAlam Jun 06 '17 at 20:04
  • 1
    @AribAlam If you're satisfied with that answer, you should accept it. That way users can get to answering other questions without trying to answer an already solved question. – Ishnark Jun 06 '17 at 20:19
  • That would have not solved the problem here, but I would add that generally speaking, using "@Override" tells the compiler what you are trying to do and let him tell you where is your mistake – Damien Jun 06 '17 at 23:05