1

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!

Ankur Saha
  • 75
  • 1
  • 8

4 Answers4

2

The compiler also adds the default constructor to any class which doesn't declare any. If your superclass declares another constructor besides the default one, and if you then comment out the default constructor, you will get the compiler error.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • You were just 32 seconds faster than me in posting this. =D +1 for the swift response. – An SO User Dec 12 '13 at 12:13
  • Hello, Mr. Topolnik, I am not getting the compiler error, i mean the doc says that i will get an error if there is no default constructor! – Ankur Saha Dec 12 '13 at 12:14
  • There is a default constructor. The compile supplies it for you. It is there even though you never see it. However the compiler will not supply a default constructor if you have defined other constructors. I think this last point is what the docs are referring to. – Alex Theedom Dec 12 '13 at 12:16
  • If you do not define a default constructor , Java magic will add it there. If you define a parameterized constructor along with a default constructor and comment it out, you will get the compiler error – An SO User Dec 12 '13 at 12:17
  • I understand why he's confused, I had that confusion too some years ago. But I hope the answers are clear now Ankur. – Jevison7x Dec 12 '13 at 12:19
  • Ok, I get it!!I will get an error when I add another constructor(parameterized) to the superclass and comment out the default constructor.. But the docs says I will get an error when the super class does not have a no-argument constructor.. It should have said that I will get an error if have another parameterized constructor along with the default one!!Thank you for your time!! – Ankur Saha Dec 12 '13 at 12:30
  • @AnkurSaha The docs are right if you know how to read them. It is guaranteed that a Java class *always has at least one constructor*, whether explicitly declared or not. – Marko Topolnik Dec 12 '13 at 12:36
0

Well in your example, if a class Student does not have any constructor, then the default no-param constructor is used, therefore the code indeed runs without a problem.

Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63
0

No argument constructor is automatically generated by the compiler unless you define another constructor.

So in your case with the student class, no argument constructor was automatically inserted because you have not provided any other constructor.

Saša Šijak
  • 8,717
  • 5
  • 47
  • 82
0

If you do not supply a no-arg constructor the compile supplies one for you, as long as there are no other constructors defined. So in your example the compiler will insert the no-arg student() constructor if you comment out you student() constructor.

Alex Theedom
  • 1,604
  • 15
  • 16