[Music] welcome to nerest technologist I'm bangar Raju and in this video I want to demonstrate you about method overloading and what are the use of it in our applications method overloading what is Method overloading method overloading means it is an approach of defining multiple methods under the class with the same name so we can Define more than one method under the class with the same name that's what we call as method overloading see suppose within a class we are defining a method like this public void test this is a method now in the same class
I can Define one more method with the same name but when I say I will Define one more method with the same name we we get a doubt here are we not going to get ambiguity yeah we'll get an ambiguity so that's what it tells is you can Define multiple methods with the same name with different parameters with different parameters what is that watch public void test in time so right now if you just understand these two are two separate methods one is a test method without parameters and one is a test method with parameter
so different parameters in the sense we can change the number of parameters that we are going to pass to the method not only like the number of parameters we can change the number of parameters we can change the type of parameters we can change the order of parameters any any of these things can be changed just think public void test one more method strings strings if you just understand this here also if you notice I'm just going to Define one more test method but now I'm passing string the number of parameters equal yeah number of
parameters are same but the difference is the type of parameters the type of parameters also can vary and uh see you can also change the order of this particular parameters here I'm defining one more method comma string us so if you just see now right now here if you notice there is a change here what is the change uh the number of parameters are changed again so one more method here string as common in time now also there is a difference what is that the order of parameters is varying if you notice here all these
methods are valid all are valid why if you compare one and two what is the difference the number of parameters are different there zero parameters here have one parameter again two and three the type of parameters are different these also valid the number of parameters the type of parameter third option here if you compare four and five the order of parameters is going to vary here we have inti strings strings inti so the parameters vary from one method to another method based on these parameters it automatically recognizes the compiler automatically recognizes which method should execute
for a particular method call okay I'll tell you we'll when we are calling we'll talk on this but see this is invalid public string test if at all you're trying to define a method like this remember this is 100% invalid why a change in the written type of a method will not be taken into consideration What It Takes into consideration is only the parameters parameters are important not the written types so here we have a change in the written type and those written types are not at all taken into consideration only the parameters of the
method will be taken into consideration why is it not valid when we are defining we'll see practically let us implement this practical and see a new project console application overload project I named it as overload project so just open a project called overload project so there's our name space and this is our class program now in this class I want to Define all the five methods what I explained you on the top so what I explained you first I want to Define all the five methods public void test I'm writing a simple code here console
do right line simply writing first method very simple code one more let me copy this and here I'm going to define a method in I so passing a parameter now and this is my second method say one more I'll Define now when I paste this particular thing here I'm getting an error I'm getting an error why am I getting because already there's a method in this class with the same parameters now see if you put it this it'll tell you type program already defines a member called test with the same parameter types with the same
parameter types there is a method so this is not taken into consideration so that's why you're getting an error but see now I'll say in I again I'll get there why there a meod with the same signature now with same parameter types one more method is also there there okay but I'm writing now strings this is valid this is valid why this is valid because the type of parameters is changing now see when you're writing the code if there is any mistake Visual Studio will tell you at the time you're writing the code only by
giving a red color underline to tell you it's an error no need to wait until compiling the code at the time of typing only it will tell you each and everything this is valid this is invalid everything it will tell you fine one more inti comma strings again the number of parameters are changing and this is my fourth method string is in time and this is my fifth method so totally five methods what I defined for you in this code all the five methods okay and now you're going to call this create the instance first
program p is equals to new program P dot when you say p dot you'll find the test method here five methods are there but it's not showing you five test methods here one only it will show you but when you select it will show you one without any parameters plus four overloads it will tell you very clearly here there are four overloads for the appropriate method okay and you can notice it the vo program. test plus four overloads in the method and when you're trying to call it when you are trying to call it see
it'll show you all the F to you in a descriptive order one of five program. test and below that you can find the rest of the use the down arrow and you can see all the overloads now second one second one which takes int as a parameter third one which takes string as a parameter fourth one which takes int and string as a parameter fifth one which takes string and it as a parameter so all the things will be shown for you here so that's how exactly Visual Studio will display the information to you fine
now I wanted to call this method now okay I'm calling now you may have a doubt which method will execute that is pretty clear which method will execute there's no confusions at all which method is going to execute the method without any parameters it executes the method without any parameters so if you run this yeah first method why first method yeah the method without any parameters is the first method now so that's executed so I'm just going to pass 10 here what is it now a method with parameter but there are four methods with parameters
but which method is going to be called the method which takes int as a parameter that is going going to be our second method I'm passing some string value XY Z third method like this see when you're going to just pass a value accordingly the appropriate method is called so we can call all the five methods now there is no confusion which method will execute see a method without parameter a method which takes inte parameter a method which takes string as a parameter a method which takes int and string as a parameter a method which
takes a string and int as a parameter totally five methods are there and you can call the appropriate method in the appropriate place okay I'll tell you just a minute why written types are not considered so we discussed return types will not be considered this is invalid why is it invalid let's understand here I'm trying to Define public string test if you're going to define a method like this and say return hello world already the compiler or Visual Studio is giving an error at the time of definition only it is giving up an error or
the error same error we already read earlier the type program already defines a member called a test with the same parameter types but still you have a doubt now return types are different now how is it going to be uh why is it going to be invalid okay let me tell you P do test just understand when you don't have the sixth method what I defined now then when we when we think like which method is called we are confidently telling the test method which doesn't take any parameters but now you cannot see why there
are two test methods which doesn't take any parameter which one will be called ambiguity there's an ambiguity between the methods now see you put your mouse here it'll tell you the call is ambiguous between the following methods are properties one is program. test second one is program test there's an ambiguity here why ambiguity because the two methods what we have here are having the same name same parameters so identification of which method has to be called is a confusion so execution will not take place for you here you may have a doubt written types are
different yes written types are different but written type of a method will come into picture in the end of execution but the confusion here is not the end here the confusion is start wait to start there is no Clarity where to start the execution when you don't know where to start the execution talking about the end of the execution is foolishness now so first thing what we require where to start we're not clear so talking about end is not a point here why where to start is not clear so that is why return types will
never be taken into consideration so that is why this is a invalid method now I'll call you all the five methods P do test which method is called the method which takes in as a parameter the method which takes string as a parameter the method which takes intend string as a parameter the method which takes string and int as a parameter all the five all the five methods executes so that is how you're going to perform the overloading of the methods why do we overload methods I'll tell you but first we learned how to overload
your methods okay now let me explain you why do we overload the methods and what is the advantage listen the concept of this overloading will fall under polymorphism of our objectoriented programming objectoriented programming is based on four principles encapsulation abstraction inheritance and polymorphism and this overloading comes under the category of polymorphism what is this polymorphism what is this polymorphism polymorphism is a mechanism of changing the behavior based on the inputs changing the behavior based on the inputs that is the input changes automatically the output or behavior changes the best example for polymorphism is ourself we
are the best example for polymorphism when you hear something which is going to be interesting and good to you you feel happy you feel happy about that but if you're hearing something which is not going to be uh with which is not going to be good you feel sad of it you feel sad of it suppose you asked your father you asked your father to purchase a bike you're asking your father to purchase a bike okay and when fine day if your father says that I'm going to purchase a bike to you so this is
something what you're expecting from your father so you feel happy for it but your father said that I'm not going to purchase a bike so I have lack of money or something some reasons I'm not going to purchase the bike to you so immediately you feel sad of it you're the same person when you receive some good input you feel happy and when you receive some bad input or which you cannot digest you're going to feel sad of it that is only what we call polymorphism behaving in different ways depending upon the input received is
what we call as polymorphism that is whenever input changes output changes right now that only I showed you see when the input is going to be integer you got one output when the input is a string you got one output when there is no input we got one output when the input is integer and string we got one output when the input is a string and integer we got one output means input is changing then automatically output is also changing see input changing output changing means it is not about changing the value means if I
change the value one output if I change another value another output for that we don't require overloading and all simple if condition is enough in a sense if I pass 10 I want one output if I pass 20 another output 30 another output for that we don't use polymorphism that all about with if conditioner switch case what we can do here input changing means the type of input can change or the number of inputs can change or the order of inputs can change what can change type of inputs or number of inputs or order of
inputs anything see here the type of inputs are changing between these two between these two the type of inputs are changing and here the number of inputs are changing no input and one input here one input in two inputs or the order of inputs are changing what that order of inputs in and string string and int like this a difference in the input what we have here will change the output that's what exactly polymorphism is whenever the input changes then automatically the output will change for you this is what we call as polymorphism okay and
we can implement this polymorphism with an approach called as method overloading that's what we are saw and tomorrow someone is asking you in an interview why method overloading is required and what is the advantage with method overloading okay what your answer should be you know method overloading is a approach of of defining multiple behaviors to a method method is same but behaviors are different simply see string actually the string is one predefined class in our language in our libraries the string is a predefined class in our libraries okay now this string class contains so many
methods in it there are so many methods present in the string class now I want to find out the location where the character o is present in the string where the character o is present in the string so the string class provides a method for me what the method index of by calling this method I can get the index now index of the character you want see index of the character you want once you're telling index of w it Returns the index of the character what the index of the character four why index starts with
zero 0 1 2 three and four that what it RS but here you'll have a doubt there are two words but written the first character yes Returns the first character only why the search always starts from zero when the search always starts from zero will stop automatically once it reaches there once it finds it the S will stop there but there's another o here I want to get the index of that how can I get the index of it for that they given you an overload method for this oh comma five what is this comma
five the comma five is nothing but the starting position of the search by default it starts from zero character but once I'm going to give five year the search is going to start from the fifth character if you start searching from the fifth character the next o is going to be seven so this is overloading see you have two behaviors for this method what is it first Behavior it will get you the first occurrence of a character second it will get you the next occurrence of a character I don't say second because if third is
present I required to just use eight there if I use eight there I'll get the third so the first occurrence and next occurrence okay here Returns what first occurrence it returns you first occurrence and when you come here this method returns you the next occurrence returns next occurrence you notice it method is same here but what is it different the parameters are different if I pass a single parameter it is written you first occurrence if I pass two parameters written you the next occurence that is what a method with multiple Behavior if you don't use
this is overloading like this what happens you know for each method I should give a different name so as a programmer what is the confusion or headache for us you know you should remember each and every name actually there are nearly five to six different overloads of the index of methods what's it character searching and string searching different different overloads are there five six methods same name the parameters will vary if you don't have overloading now the five or six meod should be given five or six names now and as a programmer we should remember
all the five or six names also so that becomes a headache for a programmer to remember that many number of names that is why method name is same simple descriptive name index of using that we can get the index of a character or we can get the in first occurrence of a character or we can get the next occurrence of a character see suppose I want to search where LL is present not L LL then s do index of now see two characters no two characters means I cannot use car method there's an overload which
takes string as a parameter there's an overload which takes string as a parameter where is it 0 1 2 the PO is two the PO is two Now understand there's index of method which takes car as a parameter there's a index of method which takes scare and int as a parameter there is index of method which Tak string as a parameter same way suppose you have another LL you have an index of method which takes string and int as a parameter so like this n number of overloads are available for the index of method each
method is used in a different scenario so this is the best example I can give you about the overloading so tomorrow someone ask you about overloading what your answer should be don't say a method with multiple methods with same name just how do you say is it is an approach of defining a method with multiple Behavior where behaviors of the methods will be changing based on the parameters of that method that is input changes output changes input means it can be either the number of values being passed to the method or type of values being
passed to the method or order of values being passed to the method thank you for more videos please subscribe to our YouTube channel [Music] Nish he