Dec 16, 2020 in JAVA
Q: Briefly explain the concept of constructor overloading.

2 Answers

0 votes
Dec 16, 2020
Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending upon the number of parameters and their corresponding types, distinguishing of the different types of constructors is done by the compiler.

class Hospital {

int variable1, variable2;

double variable3;

public Hospital(int doctors, int nurses) {

variable1 = doctors;

variable2 = nurses;

}

public Hospital(int doctors) {

variable1 = doctors;

}

public Hospital(double salaries) {

variable3 = salaries

}

}

Three constructors are defined here but they differ on the basis of parameters type and their numbers.
0 votes
Jul 27

Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending upon the number of parameters and their corresponding types, distinguishing of the different types of constructors is done by the compiler.

class Hospital {

int variable1, variable2;

double variable3;

public Hospital(int doctors, int nurses) {

 variable1 = doctors;

 variable2 = nurses;

}

public Hospital(int doctors) {

 variable1 = doctors;

}

public Hospital(double salaries) {

 variable3 = salaries

}

}

Three constructors are defined here but they differ on the basis of parameter type and their numbers.

Related questions

0 votes
Feb 18, 2020 in JAVA
0 votes
Dec 22, 2020 in Informatica
...