In this lecture we will learn:
– Parameters of a method
– Method overloading in Java
– When does method overloading happen?
– Different ways of method overloading in Java
#1
It is not compulsory that a class should have methods and variables. It will be empty also.
– The parameters that except the values in a method should be equal to the parameters that we pass in a method to call it.
– As it might create a problem if the parameters excepted by the method are not equal to the number of values passed.
– So, to overcome this problem we do overloading.
– As we can have two methods we the same name but different parameters.
– Either the number of parameters or the type of parameters should be different.
– Method with the same name and parameters but with different return types will also not work. The return type does not matter here. Method name and parameters matter only.
#2
Method overloading:- In Java, there can be more than one method with the same name but the number of parameters or type of parameters should be different.
– Method overloading is also known as Compile-time Polymorphism, Static Polymorphism, or Early binding in Java.
– In Method overloading compared to parent argument, child argument will get the highest priority.
#3
Different ways of Method Overloading in Java:-
- Changing the number of parameters
– Method overloading can be achieved by changing the number of parameters while passing to different methods.
- Changing Data Types of the Arguments
– If methods have the same name but have different parameter types then also methods are considered as overloaded.
- Changing the Order of the Parameters of Methods
– By rearranging the parameters of two or more overloaded methods.