Professional Documents
Culture Documents
Java Inheritance and Constructor
Java Inheritance and Constructor
Code:
Parent class:
package com.blogspot.adultsincebirth;
public class InheritConstructorParent
{
static
{
System.out.println("parent static block");
}
{
System.out.println("parent non static block, "+this.getClass());
}
public InheritConstructorParent()
{
System.out.println("parent ctor (NO parameter)");
}
public InheritConstructorParent(int x)
{
System.out.println("parent ctor (WITH parameter)");
}
}
Child class:
package com.blogspot.adultsincebirth;
public class InheritConstructorChild extends InheritConstructorParent
{
static
{
System.out.println("child static block");
}
{
System.out.println("child non static block");
}
public InheritConstructorChild()
{
System.out.println("child ctor (NO parameter)");
}
public InheritConstructorChild(int x)
{
System.out.println("Child ctor (WITH parameter)");
}
}
package com.blogspot.adultsincebirth;
public class InheritConstructorTest
{
public static void main(String[] args)
{
new InheritConstructorChild(0);
new InheritConstructorChild();
}
}
Output: