I'm trying to assign a sub class object array to its super class. The program compiles successfully, but I' getting an ArrayStoreException. I know that arrays parent and child are references to same array, but shouldn't I be able to access method func at least?
class Pclass
{
Pclass()
{
System.out.println("constructor : Parent class");
}
public void func()
{
System.out.println("Parent class");
}
}
class Cclass extends Pclass
{
Cclass()
{
System.out.println("Constructor : Child class");
}
public void func2()
{
System.out.println("It worked");
}
public void func()
{
System.out.println("Common");
}
}
public class test
{
public static void main(String ab[])
{
Cclass[] child = new Cclass[10];
Pclass[] parent = child;
parent[0]=new Pclass();
parent[0].func();
}
}
publicin there (and you want to fix that whitespace being all over the place... remember to read through stackoverflow.com/help/how-to-ask - the "remember to proofread" part isn't in there just for fun. Many people, you included, forget to)