Java Tutorial: Constructor Overloading with Example

Java Tutorial: Constructor Overloading with Example

We know earlier what is a constructor, default constructor, types of constructors and their advantages and place in Java language. Let us know one more concept of constructors known as constructor overloading.

Like method overloading, Java also supports constructor overloading. Writing multiple constructors, with different parameters, in the same class is known as constructor overloading. Depending upon the parameter list, the appropriate constructor is called when an object is created.

Let us see the Constructor Overloading concept programmatically.

Constructor Overloading

The Student() constructor overloading is done three times – one default constructor, the second one with string parameter and the third one with string and int parameters. In the main() method, three objects std1, std2 and std3 are created by using overloaded constructors. Depending upon the number of parameters and their data type order, the appropriate constructor is called.

The advantage or uses of constructor is it gives properties to the object at the time of object creation itself. For std2 object, name is given and std3 object name with marks are given.

Have more understanding on Constructors

View All for Java Differences on 90 Topics

Leave a Reply

Your email address will not be published. Required fields are marked *