Creating a multiple methods in a class with same name but different parameters and types is called as method overloading.method overloading is the example of Compile time polymorphism which is done at compile time.
Method overloading can be achieved by using following things :
- By changing the number of parameters used.
- By changing the order of parameters.
- By using different data types for the parameters.
- public class Methodoveloading
- {
- public int add(int a, int b) //two int type Parameters method
- {
- return a + b;
- }
- public int add(int a, int b,int c) //three int type Parameters with same method same as above
- {
- return a + b+c;
- }
- public float add(float a, float b,float c,float d) //four float type Parameters with same method same as above two method
- {
- return a + b+c+d;
- }
- }
In the above example ,their are three methods with same method same but they differ with number of parameters and type of parameters ,hence this types of methods is called as method overloading.
FAQ of Method Overloading
Question- Can method overloading has same numbers of parameters and Name with different return types?
Answer- No, because conflict is happen in methods while ing the parameters .
No comments:
Post a Comment