Hi I am Ankur and have been coding with java for few years now..I have read Head first Java The Complete Reference by Herbert Schildt earlier and recently I came across a major fallacy in this page of Oracle docs.The note section says If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem. But if I explicitly do not mention a default constructor in a superclass I still do not get an error!I never found such an information in either of the books mentioned!!In fact I can easily run a program exhibiting constructor chaining without supplying a default superclass constructor!! Here is the code:
import java.io.*;
import java.util.*;
class student
{
// student()
//{
//System.out.println("I am student\n");
// }
public void subjects()
{
System.out.println("English..");
}
}
class engineering extends student
{
engineering()
{
System.out.println("I am an engineer..\n");
}
public void subjects()
{
System.out.println("Maths");
}
}
class computer extends engineering
{
computer()
{
System.out.println("I am a computer engineer.");
}
public void subjects()
{
System.out.println("Computer");
}
}
class test8
{
public static void main(String args[])
{
computer cs=new computer();
}
}
In the above code if I run it with the commented block of code, constructor chaining occurs normally.First the student() constructor runs, then the engineering() and then computer()..But when i remove the default constructor of the base class student(), it still runs successfully..Isn't the argument in oracle docs that during constructor chaining one has to supply the default constructor in a superclass wrong or is my understanding with this section in oracle docs wrong?Please help me with this inconsistency!! Thank You!