I always thought that constructors aren\'t inherited, but look at this code:
class Parent {
Parent() {
S
Constructor are not inherited.
Super class constructor are not inherited in derived class.
Is there any possibility to create 2 constructors without parameters and have only Child constructor on result without base constructor.
No, Its not possible In Java every derived class constructor call super class constructor. If you not add it call no argument constructor.
public SuperClass() {
...
}
public DerivedClass() {
//Compiler here call no argument constructor of Super class.
}
public class HelloWorld{
private static class A {
A(String x) {
System.out.println(x);
}
}
private static class B extends A {
}
public static void main(String []args){
B b = new B();
System.out.println("Hello World");
}
}
Output:
/*
error: constructor A in class A cannot be applied to given types;
private static class B extends A {
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
1 error
*/
So what it means is constructors are never inherited
Hence, what happens here is class B's argument less constructor tries to call class A's argument less contructor, But it didn't find that hence gives it gives error.
If we add argument less constructor in class A, then this program will work properly.
Whatever you are seeing here is called as constructor chaining. Now What is Constructor Chaining:
Constructor chaining occurs through the use of inheritance. A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.
There could be any number of classes in an inheritance chain. Every constructor method will call up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.(Source)
That's what happening in your program. When you compile your program , your Child
is compiled to this way by javac
:
class Child extends Parent
{
Child()
{
super();//automatically inserted here in .class file of Child
System.out.println("S2");
}
}
And your Parent class is converted to following:
Parent()
{
super();//Constructor of Object class
System.out.println("S1");
}
That's why your output is showing as:
S1 //output from constructor of super class Parent
S2 //output from constructor of child Class Child
A constructor will always call its superclass constructor unless an explicit constructor has been defined. From the Java Language Specification:
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.
If superclass doesn’t have default constructor, then subclass also needs to have an explicit constructor defined. Else it will throw compile time exception. In the subclass constructor, call to superclass constructor is mandatory in this case and it should be the first statement in the subclass constructor.
You write:
It shows that Child inherited constructor.
Constructors can not be inherited. Classes can be inherited, so Child does not inherit any constructor. Child inherits class Parent. Parent inherits class Object. When you call the Child constructor, automatically an Object constructor is called and then a Parent constructor, before the code of the Child constructor is run.
This why you get this result:
S1
S2