at the moment, I am wondering if there is a way to add a child class object to a parent class array.
I have code that follows the generic line of:
public abstract class Parent {
...
}
public class Child extends Parent {
...
}
And I have an array that follows along the lines of:
Parent[] array = new Parent[number];
I want to be able to add my child objects to this array like:
array[0] = new Child();
However whenever I do this, I get an error message saying that they are incompatible types. I know this can be achieved in ArrayList but I want to see if it is possible in the format above. Is there a way to achieve this without ArrayList?
Parent[] array = new Parent[];I suspect your code is different which is the reason it doesn't compile.