Method Overloading and Overriding
Method Overloading and Overriding
Overriding
Let’s first discuss about
Method Overloading
What is Method Overloading?
• If a class has multiple methods having same name but
different parameters, it is known as Method
Overloading.
Why we need it?
• Suppose you have to perform addition of two numbers but
there can be any number of arguments as well as
difference in datatypes. So as a programmer what would
you do? Write different methods for different number of
arguments and data types? For example a(int a, int b),
b(int a, int b, int c), c(double a, double b), and so on.
Doing this makes it harder for you and other programmers
to understand the behavior of the method because its
name differs.
• So to resolve this problem we have Method Overloading.
Advantage of Method
Overloading
• Method Overloading increases the readability of the
program.
• Method overloading give programmers the flexibility to
call a similar method for different datatypes.
Different ways to overload a
method
• By number of arguments
• By changing the datatype.