- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different ways to overload a method in Java
Method overloading can be achieved in following three ways −
- By changing the number of parameters in the method.
- By changing the order of parameter types
- By changing the data types of the parameters.
See the example below−
Example
public class Tester { public static void main(String args[]) { Tester tester = new Tester(); System.out.println(tester.add(1, 2)); System.out.println(tester.add(1, 2,3)); System.out.println(tester.add(1.0f, 2,3)); System.out.println(tester.add(1, 2.0f,3)); } public int add(int a, int b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; } public float add(float a, int b, int c) { return a + b + c; } public float add(int a, float b, int c) { return a + b + c; } }
Output
3 6 6.0 6.0
Advertisements
To Continue Learning Please Login
Login with Google