0

I have an ArrayClass and mergeSortArray extends it. And mergeSortArray contains a mergeSort() method. However, since I used super to call a constructor from the superclass, I do not know how to refer to the mergeSortArray (the subclass object / array) and pass it as a parameter in the mergeSort method. In fact, is this even feasible ? I know I can do this in a NON- OOP way. However, I am keen to know how to do this in an OOP way.

Please correct me if I have said incorrect, as I am new to Java and I want to learn more about it.

// ArrayClass Object
    import java.util.*;
    import java.io.*;
    import java.math.*;

public class ArrayClass{
    public int[] input_array;
    public int nElems;

    public ArrayClass(int max){
        input_array = new int [max];
        nElems = 0;
    }

    public void insert(int value){
        input_array[nElems++] = value;
    }

    public void display(){
        for(int j = 0; j < nElems; j++){
            System.out.print(input_array[j] + " ");
        }
        System.out.println("");
    }

}



import java.io.*;
import java.util.*;
import java.math.*;

class mergeSortArray extends ArrayClass{

    public mergeSortArray(int max){
        super(max);
    }

    public void methodOne(){
        int[] output_array = new int[super.nElems];
        mergeSort( // ************* // ,output_array,0, super.nElems -1);

     }
       ................
}

I am not sure what I should put to replace ****** such that I can pass mergeSortArray as a parameter into the mergeSort method.

2 Answers 2

1

There isn't a mergeSortArray. You inherit input_array like (and no need for super.nElems you inherit that too),

 mergeSort( input_array, output_array, 0, nElems - 1);

Your sub-class will inherit everything that is protected or greater visibility (not private), however your ArrayClass gives you both public fields

public int[] input_array;
public int nElems;

They should probably be protected and have accessor methods (getters).

protected int[] input_array;
protected int nElems;
public int size() {
  return nElems;
}
public int[] getInputArray() {
  return input_array;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Elliott_Frisch If the variables are protected in the ArrayClass, the sub-class can access them (as they are not private). In this case , why would I still want to specify public int size() and public int[] getInputArray() instance method ?
Those are accessor methods. It's generally preferred over making the fields public (like they were in your question).
but if the fields are protected, I can access them without accessor methods. Then why need accessor methods ?
1

First of all, I suggest you not to have public fields on OO code. You got two public fields (input_array and nElems), you should change them to private and create acessors if you need.

Then, to refer to those fields on the subclass, you can either use a protected acessor if you wish to hide the acessor from the rest of the API or a public one, if it's part of your public API. That way, on your ArrayClass:

public class ArrayClass {
    private int[] input_array;
    private int nElems;

    //this may be public
    protected int[] getInputArray() {
        return input_array;
    }

and when you get to call your mergeSort method, you can use getInputArray()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.