Here's how to do it:
public class MainTest { class Employee { private int id; public Employee(int id) { super(); this.id = id; } } class TechEmployee extends Employee{ public TechEmployee(int id) { super(id); } } public static void main(String[] args) { MainTest test = new MainTest(); test.runTest(); } private void runTest(){ TechEmployee[] temps = new TechEmployee[3]; temps[0] = new TechEmployee(0); temps[1] = new TechEmployee(1); temps[2] = new TechEmployee(2); Employee[] emps = Arrays.copyOf(temps, temps.length, Employee[].class); System.out.println(Arrays.toString(emps)); } }
Just remember that you cannot do it the other way around i.e. you cannot convert Employee [] to TechEmployee [].
source share