Constructors don’t have a return type and they should be as the same as the class name.
Constructors are used to initialize the object’s state.
When is a Constructor called ?
Each time an object is created using new() keyword at least one constructor (it could be default constructor) is invoked to assign initial values to the data members of the same class.
A constructor is invoked at the time of object or instance creation.
Rules of writing a constructor:
>Constructor(s) of a class must has same name as the class name in which it resides.
>A constructor in Java can not be abstract, final, static and Synchronized.
>Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.
Types of constructors:
Constructors without arguments:
A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class. And if we write a constructor with arguments or no-arguments then the compiler does not create a default constructor.
Default constructor provides the default values to the object like 0, null, etc. depending on the type.
Constructors with parameter:
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.
constructors only return current class instance and we can still write the return word inside the constructor.
lets try it another way:
Reason for this error is that we have already made another constructor taking parameters as the order of the new constructor. So the compiler can’t sort out wether we are initializing an object by entering kind/type and height/age. That’s fair enough.