Showing posts with label Constructor Overloading. Show all posts
Showing posts with label Constructor Overloading. Show all posts

Constructor Overloading in C sharp

Constructor Overloading


  • Just like we can overload methods in a class we can also overload constructors of class that is a class can be defined with more than one constructor under it.
  • If a class is defined with multiple constructor then instance of the class can be created by using any available constructor. it is not mandatory we need to create the instance by using any particular constructor.
Add a class ‘LoadCon.Cs’ and write the following code


By overloading constructor of a class we can initialized variables of the class in three different ways.
  1. With the help of parameters constructor, we can initialize variable of that class with default values, that is the reason why Parameters constructor we also known as default constructor.
  2. With the help of parameterized constructor we can either initialize all the variables of that class with given values or some variable with given and some variable with default values.
  3. Inheritance based overloading,It is possible to overload a method of parent class under its child and we call this as inheritance based overloading.





Polymorphism in C sharp

Polymorphism


  • It is an approach in behaving in different way depending upon the received input that is whenever the input change then automatically the output or behaviors  will change.
  • We can implement polymorphism into our application by using three different approaches
  1. Overloading
  2. Overriding
  3. Hiding/Shadowing
Overloading : - This is again three types
  1. Method Overloading
  2. Constructor Overloading
  3. Operator Overloading
Method Overloading : It’s an approach of defining multiple method under a class with the same name changing the parameter of that method that is we can change either the number of parameters passed to the method or type of parameter passed to the method or order of parameter passed to the method.

Example : 


Note :  A change in the return type of method will not be taken into consideration in overloading.

Add a class "Loadmethods" and write the following code:

In overloading behavior or output of methods will change based on the parameter value of that method that is type of value, number of value as well as order of values.

Method overloading is an approach of defining multiple behaviors to a method and that method start behaving in different ways based on the parameter of that method.

Example

In the above case the IndexOf () method of string class will return the index of a particular character under the string but the difference is the first method return  the first occurrence of the character and the second method returns the next occurrence of the character so in this case the name of method is same. The only difference is the parameter of the method.

The WriteLine () method of the console class is also a good example for overloading because the method has 19 different overloads can print any value that passed as parameter to the method.