Let's once more return to our Person class. It currently looks like this: Show
All person objects are 0 years old when created. This is because the constructor sets the value of the instance variable
Constructor OverloadingWe would also like to be able to create persons so that the constructor is provided both the age as well as the name as parameters. This is possible since a class may have multiple constructors. Let's make an alternative constructor. The old constructor can remain in place.
We now have two alternative ways to create objects:
Sample output Paul is 24 years old. Ada is 0 years old. The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters. We cannot, for example, add a Calling Your ConstructorHold on a moment. We'd previously concluded that "copy-paste" code is not a good idea. When you look at the overloaded constructors above, however, they have a lot in common. We're not happy with this. The first constructor - the one that receives a name as a parameter - is in fact a special case of the second constructor - the one that's given both name and age. What if the first constructor could call the second constructor? This is possible. A constructor can be called from another constructor using the Let's modify the first constructor so that it does not do anything by itself, but instead calls the second constructor and asks it to set the age to 0.
The constructor call New objects can be created just as before:
Sample output Paul is 24 years old. Eve is 0 years old. Loading Method OverloadingMethods can be overloaded in the same way as constructors, i.e., multiple versions of a given method can be created. Once again, the parameters of the different versions must
be different. Let's make another version of the
In the example below, "Paul" is born 24 years old, ages by a year and then by 10 years:
Prints: Sample output Paul is 24 years old. Paul is 25 years old. Paul is 35 years old. A Person now has two methods, both called We may also modify the program so that the parameterless method is implemented using the method
Loading What is it called when two methods have the same name?Having two or more methods named the same in the same class is called overloading.
What is it called when two methods have the same name but different parameters?The practice of defining two or more methods within the same class that share the same name but have different parameters is called overloading methods.
What is a method where a class has two or more methods by the same name but different parameters?If a class has multiple methods having same name but parameters of the method should be different is known as Method Overloading.
What is it called when you have multiple methods of the same name but have different numbers or types of parameters?If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
|