Welcome back friends now in a hurry in this lecture we're going to learn about polymorphism which means one name many form. And there might be different functions or methods out there and programs with the common name the same name, but they may have different functionality, this is called polymorphism. We can implement polymorphism in object oriented programming approach, it could not be done in traditional approach. We can create a while loading of methods, constructors and different things. So let's start. Here we have method overloading, which is an implementation of polymorphism.
If a class has a multiple methods having same name, but different in parameters, they differ in number of parameters. properties of parameters are more things, which is called method overloading. We can implement methods coding in two ways in Java, first by changing number of arguments. If the initial method have a two arguments, we can implement by changing the number of arguments, say three, zero or one anything, they may not have the same number of callbacks. Or the second method is by changing the data type. The first method has a data type integer, we can change the second method with a float or double to have support of same numerical values.
So we are going to learn each one of them in detail. Here is an example of method overloading and the case without method overloading. Here are two programs on the left hand side in the orange color. We have a program of implementation of three methods without method overloading. They perform the same operation of addition of variables and return the sum to the users For that reason, we have created three different functions or methods Add to add three, add four all have different names, but perform the same operation. They only differ in the number of variables as input perform the same reason why with overloading we can have the same name, method name for performing different operations on the properties is different.
So, if a user provided any input, he meant a two number three number of four numbers, we do not know we could not define it earlier in the development time. So, we need to call all three functions three methods and pass the user input value to all the three and if anyone matches we can get a result, so it would be more tedious or memory intensive task. With method overloading, we only need to provide only a single method. It will take the desired outcome operations, it will pass to the desired or destined method and it will compute the result easily. So, we only need to write single name once, we don't need to write three methods. Similarly for programming skills, this will be very useful, the overloading will be useful.
In cases we want to perform various operation However, we do not know which method to call Exactly. And also if you are working in a development environment working with a team, we also need to have a lot of methods with the same name. To avoid errors or compile time errors or different sort of errors, we can have overloading. Then there is a constructor overloading. constructor is an instance of class with the same name, as the class name doesn't have any return type as it is a default. You can create overloading of consecutive as well.
We may have different variables, it will differ with a zero parameter constructor, one parameter, two parameter multiple parameter, it may have a copy parameter, starting address of another variable or the data constructor. So, how we can implement overloading the first method is changing number of arguments. And we have a class here called adder, which have two methods. Both are static in type, the return type is integer, the name of method is same ad and perform the operations. The only difference between those methods is the number of arguments that we have. We can create another class, I define the public static void Main method, and we can call those methods using the object of the previous class.
All directly we can automatically call using the class name. Similarly, now, we can also implement overloading by changing the return type. In the initial method, we it is when historian the interior format and the preceding method, the same name is implemented using different data types a double float or more. So, it will automatically identify which method to call is a data provided by the user is in format of integer the integer method will be called and if it will be provided as float or double value in decimal place value, it will be passed to the double method. So, this was a lot of method overloading, we can implement new solutions with method overloading and cachaca overloading. overloading is an important topic.
Are when you're trying to create different set of classes or implement polymorphism in the advanced programs, try implementing your own solution. Till then keep learning and keep moving ahead