Hi,
public class A
{
public A() // Class A's constructor
{
super();
System.out.println("instantiate a parent");
}
}
public class B extends A // Class B with no constructor
{
public B()
{
super();
} public void child () // Not a constructor
{
System.out.println("instantiate a child");
}
}
public class C
{
public static void main(String [] args)
{
A a = new B();
}
}
1)In
Java whenever you do not provide a constructor for a class compiler inserts the default contructor with a call to super class constructor in your class.
2)Now, due to inheritance whenever you create a subclass's object it first invoke its super class's contructor and after the superclass object creation completes then subclass constructor is executed.
In your program 1) & 2) both are valid as you don't have a constructor for a subclass B which is extending from class A. So when you write
A a= new B();
compiler invokes its default constructor in which there is (no print system)just a call to super, so it will print
instantiate a parent nothing more.
Since
public void child(){System.out.println("instantiate a child");} never gets executed, you will never see this message printed on the screen.
Hope this helps you.
Asha