Constructor Overloading

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 4

CONSTRUCTOR OVERLOADING

DEFINITION

 In Java, we can overload constructors like methods.

 The constructor overloading can be defined as the concept of having more than one constructor with different
parameters so that every constructor can perform a different task.

 Here, we need to understand the purpose of constructor overloading. Sometimes, we need to use multiple
constructors to initialize the different values of the class.

 We must also notice that the java compiler invokes a default constructor when we do not use any constructor in the
class. However, the default constructor is not invoked if we have used any constructor in the class, whether it is
default or parameterized. In this case, the java compiler throws an exception saying the constructor is undefined.
CONSTRUCTOR OVERLOADING

Use of this () in constructor overloading

• we can use this keyword inside the constructor, which can be used to invoke the other constructor of the
same class.

• this() is used in parameterized constructor to call default constructor/zero parameter constructor


EXAMPLE

• Examples of valid constructors for class Account are

• Account(int a);

• Account (int a,int b);

• Account (String a,int b);

You might also like