0% found this document useful (0 votes)
24 views19 pages

Method Overloading and Overriding

Uploaded by

hinatahyuga56890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views19 pages

Method Overloading and Overriding

Uploaded by

hinatahyuga56890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Method Overloading / Method

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.

“Why Method Overloading is not possible by changing return type of method


only?”
(we will discuss shortly)
Method Overloading changing
no. of arguments

• We have created two


methods, first add()
methods performs
addition of two
numbers and second
add() method performs
addition of three
numbers.
Method Overloading changing
type of arguments

• We have created two


methods that differs in
data type. The first
add() method receives
two integer arguments
and second receives
two double arguments.
Now back to our question.
• Q: Why Method Overloading is not possible by changing
the return type of method only?

• Answer: In java, method overloading is not possible by


changing the return type of the method only because of
“ambiguity”. Lets take an example:
Now back to our question
(cont…)
Now let’s discuss about
Method Overriding
What is Method Overriding?
• If a subclass provides the specific implementation of the
method that has been provided by its parent/super class,
it is known as Method Overriding.
OR
• If a method of subclass has the same signature (name and
parameter list) and return type as that of method is super
class then we can say that the methods are overridden.
Example
Why we need it?
• Method overriding is used to provide specific
implementation of a method that is already provided by its
superclass.
• It is also used to achieve run-time polymorphism.
Rules for Method Overriding
• Method must have same name as in parent class.
• Method must have same parameter list as in parent class.
• Must have same return type as in parent class.
• Must be a IS-A relationship (Inheritance).
• Final methods cannot be overridden.
• Static methods can not be overridden.
• Private methods cannot be overridden.
Rules for Method Overriding
(Cont…)
• Cannot override constructor as parent and child class
can never have constructor with same name (constructor
name must always be same as Class name).
Usage of super keyword
Usage of super keyword
Any Question?

You might also like