I have an array of objects, and I want to use a setter function to update one of them. However, the array's type is of the parent class, which doesn't have the setter function. So I type casted the array to the correct class, like this example:
class Parent {
}
class Child : Parent {
private int n = 0;
//function specific to Child
public void updateN(int number)
{
n = number
}
}
class Main {
static void main(string[] args)
{
Parent[] a = new Parent[];
a[0] = new Child();
Child c = (Child)a[0];
c.updateN(1);
}
}
Now, does the code at the bottom update the Child object on the array? Or does it only update the object stored in variable c
. If not, whats the best way to update the object in the array?