Re: Array to subclass
Mike wrote:
I am new to this and i don't understand how to transfer my arrays to a
subclass, alter the data and then send it back to the superclass. I
don't want to jump right in, I do some manipulating on the arrays in
the superclass using several methods. I want to pick the point in the
supercalss whent it sends to the subclass and I would like the altyered
data to drop back to the superclass right after the call to subclass.
I'm not sure how the classes are structured, but it's easy to do this
if your superclass is abstract.
public abstract class Super {
public void process(Object[] array) {
// Do a lot of work on the array here
subProcess(array);
// Do even more processing here
}
protected abstract void subProcess(Object[] array);
}
public class Sub extends Super {
protected abstract void subProcess(Object[] array) {
// Do sub processing here
}
}
The abstract makes sure the Sub class has to implement a certain method
for it to compile. You can easily create different subclasses which
perform different processing:
public class OtherSub extends Super {
protected abstract void subProcess(Object[] array) {
// Do other sub processing here
}
}
public class Tester {
public static void main(String[] args) {
String[] myArray = new String[] { "a", "b", "c"};
Super one = new Sub();
Super two = new OtherSub();
one.process(myArray.clone());
two.process(myArray.clone());
}
}
I hope the examples show the power of using an abstract superclass.
Regards,
Bart