
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
- Related Articles
- Can we overload Java main method?
- Can we overload the main method in Java?
- Can we overload a method based on different return type but same argument type and number, in java?
- Can we overload or override a static method in Java?\n
- Different ways to concatenate Strings in Java
- Different ways to create Objects in Java
- 5 different ways to create objects in Java
- Different ways to print exception messages in Java
- Different ways to traverse an Array in Java?
- Different ways to create an object in java?
- What is overloading? What happens if we overload a main method in java?
- Different ways for Integer to String conversion in Java
- What are the different ways for a method to be overloaded in C#?
- Different ways to format long with Java System.out.format
- How many ways to prevent method overriding in Java?

Advertisements