0 ratings0% found this document useful (0 votes) 28 views7 pagesQ 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
2arive023 14:25 Java Method Overloading
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
schools
Java Method Overloading
[crv] Looe |
Method Overloading
With method overloading, multiple methods can have the same name with different
parameters:
Example
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)
Consider the following example, which has two methods that add numbers of different
type
Example
static int plusMethodInt(int x, int y) {
return x + y3
static double plusMethodDouble(double x, double y) {
return x + y5
hitpsswwschools.comfavaljava_methods_overloading asp we2arive023 14:25, Java Method Overloading
3
WW tutoriaisy Exercises» Servicessy§ Q 0
schools
IPT SQL. PYTHON PHP
system.out.println("double: " + myNum2) ;
Instead of defining two methods that should do the same thing, it is better to
overload one.
In the example below, we overload the plusMethod method to work for both int
and double:
Example
return x + ys
static double plusMethod(double x, double y) {
public static void main(String[] args) {
int myNum1 = plusMethod(8, 5);
double myNum2 = plusMethod(4.3, 6.26);
system.out.printIn("int: "+ myNum1) ;
Note: Multiple methods can have the same name as long as the number and/or type
of parameters are different.
hitpssvawwschools.comfavaljava_methods_overloading asp ar2arive023 14:25,
w
schools
htipsitinww.w3schools comvaval
Tutorials
Exercises ¥
sQu
Java Method Overloading
Servicesy Q 0
PYTHON PHP.
ADVERTISEMENT
methods_overloading asp
H
WTO
Login
w3.css
an2arive023 14:25, Java Method Overloading
3
w Tutorials Exercisesw Servicesey§ QO Login
NW a€
eee
schools
See eC
Peco nears ern
COLOR PICKER
hitpsslvaw.wschools.comfaval
fa_methods_overloading asp
an2arive023 14:25, Java Method Overloading
v’ Tutorialsy —Exercisesw Servicese§ QO Log in
W3Schools
Spaces
Build Your
Web, Mobile, and
Desktop Solutions
Get Premium!
a
Exetel) Ey
ADVERTISEMENT
ADVERTISEMENT
hitpssvawwschools.comfavaljava_methods_overloading asp sr2arive023 14:25, Java Method Overloading
Tutorials» —Exercisese Servicese§ QU O SignUp Login
CSS JAVASCRIPT SQL PYTHON «= JAVA. PHP-—«S HOWTO —W3.CSS.
3
Ww SPACES UPGRADE NEWSLETTER
schools
GET CERTIFIED REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
cH Tutorial
Query Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
Wa.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
‘Angular Reference
jQuery Reference
Top Examples Get Certified
HTML Certificate
htipsilweww.w3schools comavaljava_methods_overloading.asp2arive023 14:25, Java Method Overloading
3
WW tutoriaisy — Brercisese Servieesey Q O SignUp Login
schools
= CSS JAVASCRIPT SQL. PYTHON = JAVA. PHP-—«S« HOWTO _—W3.CSS.
Wa3.cSS Examples Query Certificate
Bootstrap Examples Java Certificate
PHP Examples c++ Centficate
Java Examples i Certificate
XML Examples XML Certificate
Query Examples
G@ @& B® © Forum asour
W3Schools Is optimized for learning and training. Examples might be simplified to
improve reading and learning
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by
W3.CSS,
htipsilweww.w3schools comavaljava_methods_overloading.asp
a