class Alpha
{
String name = "Alpha";
Alpha()
{
print();
}
void print()
{
System.out.println("Alpha Constructor");
}
}
class Beta extends Alpha
{
int i = 5;
String name = "Beta";
public static void main(String[] args)
{
Alpha a = new Beta();
a.print();//Line1 executes Beta constructor
System.out.println(a.name);//Line 2 displays Alpha instance variable
}
void print()
{
System.out.println(i);
}
}
This program compiles successfully and displays the following output.
0
5
Alpha
Questions
a) I dont understand why Alpha's constructor did not get executed first.
I believe "super()" will be called implicitly by every child constructor first ...right ?.
b) If Beta's constructor is already executed then why "5" is printed ? (Second Line in Output)
The third line I kinda understand (i.e Alpha's own variable will be displayed because casting is not yet done on "a" instance variable)