You cannot add members : methods or fields to an array. So no, it is not possible :
SubClass[] subArray = superArray.asAnArrayOfSubClass();
To define a behavior for an array, instead create a method where you pass the array :
SubClass[] doThat(SuperClass[] superArray){
....
}
Even if conceptually, a type should not know its subtypes, if it is your requirement it is valid to convert manually an array of a specific type to an array of a subclass of this specific type.
For example, you can have employees stored in an array that at time are promoted as managers. This method could do this conversion :
Manager[] promote(Employee[] employees){
Manager[] managers = new Manager[employees.length];
for (int i=0; i<employees.length; i++){
Employee e = employee[i];
managers[i] = new Manager(....);
}
return managers;
}
SubClass[] subArray = (SubClass[]) superArray, but it won't work; it'll give you aClassCastException. So the real question is: What are trying to achieve?ArrayList, for example.Vectorand sub classesRowandColumn, I wish to initialize a matrix either by inputting rows or columns. I just wondered if it was possible to first create an array of the "Vector" Class and then cast it to a Row or Column array. Might not be the optimal way of doing this, but this is for learning purposes anyway