Java OOP: referencing subclass object -
i have arrayclass
, mergesortarray
extends it. , mergesortarray
contains mergesort()
method. however, since used super
call constructor superclass, not know how refer mergesortarray (the subclass object / array) , pass parameter in mergesort
method. in fact, feasible ? know can in non- oop way. however, keen know how in oop way.
please correct me if have said incorrect, new java , want learn more 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 not sure should put replace ******
such can pass mergesortarray
parameter mergesort
method.
there isn't mergesortarray
. inherit input_array
(and no need super.nelems
inherit too),
mergesort( input_array, output_array, 0, nelems - 1);
your sub-class inherit protected
or greater visibility (not private
), arrayclass
gives both public fields
public int[] input_array; public int nelems;
they should protected
, have accessor methods (getters).
protected int[] input_array; protected int nelems; public int size() { return nelems; } public int[] getinputarray() { return input_array; }
Comments
Post a Comment