0% found this document useful (0 votes)
2 views11 pages

Lecture 12 Overloading (1)

The document discusses method overloading in Java, explaining that multiple methods can share the same name if they differ in parameters. It highlights the benefits of overloading, such as improved readability and the ability to invoke a single method. Additionally, it covers how overloaded methods are resolved by matching arguments and mentions that the main() method can also be overloaded, though the JVM looks for the standard main method to start execution.

Uploaded by

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

Lecture 12 Overloading (1)

The document discusses method overloading in Java, explaining that multiple methods can share the same name if they differ in parameters. It highlights the benefits of overloading, such as improved readability and the ability to invoke a single method. Additionally, it covers how overloaded methods are resolved by matching arguments and mentions that the main() method can also be overloaded, though the JVM looks for the standard main method to start execution.

Uploaded by

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

OVERLOADING

LECTURE 12
BITS-PILANI,K.K. BIRLA GOA CAMPUS

Prof. Anita Agrawal


Method Overloading???
2

 In Java, two or more methods can have the same


name if ……
 They differ in parameters:
◼ different number of parameters, Overloaded
◼ different types of parameters, methods
◼ Both of the above

Method overloading

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Example:
3

void func() void func(double a)


{ {
... ...
} }
void func(int a) void func(int a, int b)
{ {
... ...
} }

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Example:
4

void func(int a) int func (int a)


{ {
... ...
} }

Overloaded methods may or may not have different return type,


but they must differ in arguments they accept.

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Method overloading, why???
5

 It improves the readability of the program.

 Only one method needs to be invoked

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


6

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Different ways to perform method
7
overloading…..

 Overloading by changing number of arguments

 Overloading by changing the type of arguments

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Method 1
8

 By changing the number of arguments:


Ex: Write down a java program for class Honda using
method overloading to display the following output for
city.

Color is Golden_brown
Model is VCVT Type is Sedan

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Method 2
9

 By changing the type of arguments

Ex: Write down a java program for class Honda using


method overloading to display the following output for
city:

Color is: Radiant_Red_Metallic


Autotransmission? True

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Method Overloading…
10

 When an overloaded method is called: –

 Java first looks for an exact match between the


arguments and method’s parameters
◼ Number and type is matched
◼ If exact match method is not found
◼ Automatic type conversion will take place->
Overload resolution

Anita Agrawal CS F213 2/11/2023 3:29:56 PM


Main overloading
11

 Apart from the fact that main() is just like any other
method & can be overloaded in a similar manner, JVM
always looks for the method signature to launch the
program.

 The normal main method acts as an entry point for the


JVM to start the execution of program.

 The program doesn’t execute the overloaded main


method when we run the program, we need to call the
overloaded main method from the actual main method
only.
Anita Agrawal CS F213 2/11/2023 3:29:56 PM

You might also like