-> For the successful creation of the subclass object, at least one of the constructors of the super class must be executed. It may of zero argument type or of parameterized type.
->in the sub class constructor, java compiler writes super() implicitly in the first line . if we don’t write explicitly. This is known as implicit calls to the zero argument constructor of the super class, in such case, it is mandatory to have zero argument constructor in the super class.
->in the above 2 examples as zero argument constructor is not available. In super class and more over we are depending upon the compiler provide super() , we are getting compilation error.
->we can fix the error in 2 ways.
1)
Define a zero argument constructor explicitly or let the compiler provide zero argument constructor implicitly in the super class.
2)
From within the sub class constructor make an explicit call to the super class constructor as fallows.
constructortwo(int b)
{
Super(10); //explicit call to the super class constructor.
System.out.println("constructor of sub class");
}
Comments
Post a Comment