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
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.
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.
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.
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.