What is the purpose of a no-arg constructor?

(a)The no-arg constructor you are referring to is a explicitly defined substitute to the “default constructor”.If the programmer doesn’t explicitly define a constructor,then the compiler(javac) automatically defines a default constructor as ClassName(){}.This constructor is not visible in the code,however after the compilation is done,it is present in the java bytecode.

(b)In your case, there is a explicitly defined no-arg constructor.The reason being that you have explicitly defined a parameterised constructor
public Employee(String name, String jobTitle, int salary) ,
public Person (String name).
If parameterised constructors are defined explicitly in a program the Java compiler doesn’t insert the implicit default constructor. So, if you wish to make the object of the class without passing any initial values Employee president = new Employee(); This statement would throw an errorNo default constructor found; nested exception is java.lang.NoSuchMethodException:.Thus there are explicit definitions for the no-arg constructors to support such object creation, where a user may allocate heap space for a object first, and then initialise it later using the setter methods or some other mechanisms.

Leave a Comment