Constructor Chaining in Java

Constructor chaining is the way through which a constructor make a call to another constructor. Constructor chaining can be done in 2 ways: Output In the above code we called Example2() constructor for object creation which in turn calls Example2(int age) constructor using this(29). Now inside Example2(int age) constructor a call is made to Example2(int … Read more

Constructor Overloading

Constructor overloading is the way through which multiple constructors can be defined within the class by changing the arguments of the signature. Let us understand this with an example: Output In the above code we have overloaded the constructor by changing the number of parameters in signature. We can use any constructor for creation of … Read more

Parameterized Constructor in Java

Parameterized constructor can be made by the user if they want to assign some value to instance variable at time of creation of object. Parameterized constructors are also a good way to perform some operations that are to be performed at the start such as Database connections, pre-checks on values of instance variables or certain … Read more

Default Constructor in Java

Default constructor is automatically created by the compiler if we do not declare any constructor. The features of default constructor are : Let us look at an example to understand default constructor: Output As you can see in the above code we have not created any constructor in our class TestA, still instance variables age … Read more