Can a parent class array hold children class objects in Java? -
i have parent class named student. have created 2 children class permanentstudent , casualstudent extending this. have written constructors both extended child classes (both child class have own constructors). now, making array of size 10 of students out of 4 permanent students , 6 casualstudents. this, did follows:
student[] = new student[10]; int count;
now, wish fill array 4 permanent students objects , 6 casual student objects information through constructor. following,
for (count = 0; count < 4; count++) { a[count] = new permanentstudent(a,b,c); // invoking constructor } (count = 4; count < 10; count++) { a[count] = new casualstudent(x,y); // invoking constructor of other class }
but gives me compilation error. going wrong in this? thanks!
assignment base base = new sub()
or base[i] = new sub()
in case legal in java. problem in constructors, check no-arguments constructor exists in student
.
Comments
Post a Comment