Constructor Overloading
Constructor oveloading enables to have constructors with different sets of parameters.
Example below:
Example below:
public class Employee
{
public Employee()
{
// This is the no parameter constructor method.
// First Constructor
}
public Employee(int Age)
{
// This is the constructor with one parameter i.e. Age.
// Second Constructor
}
public Employee(int Age, string Name)
{
// This is the constructor with two parameters i.e. Age and Name.
// Third Constructor
}
}
Call to the constructor now depends on the way you instantiate the object.
For example:
Employee objEmp = new Employee()
// At this time the code of no parameter
// constructor (First Constructor)will be executed
Employee objEmp = new Employee(12)
// At this time the code of one parameter
// constructor(Second Constructor)will be
// executed.
No comments:
Post a Comment