- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Method Overloading
In Java, you can define two or more methods in the same class that share the same name, as long as their parameter declarations are dissimilar. When this is the case, then the methods are said to be overloaded, and the process is referred to as the method overloading.
Method overloading is one of the way that Java supports polymorphism. If you have never used a language that allows method overloading, then the concept may seem strange for you. But you will see, method overloading is one of the Java's most exciting and useful features.
Java Method Overloading Rule
When an overloaded method is invoked, Java uses the type and/or the number of arguments as its guide to determine which version of the overloaded method to actually call. Therefore, overloaded methods must differ in the type and/or the number of their parameters. While overloaded methods may have dissimilar return types, the return type alone is insufficient to distinguish the two versions of method. Whenever Java encounters a call to an overloaded method, it is simply executes the version of the method of which the parameters match the arguments used in the call.
Java Method Overloading Example
Following is a simple example program that illustrates the method overloading in Java:
/* Java Program Example - Java Method Overloading * This program demonstrate the method overloading * concept of Java */ class OverloadDemo { void test() { System.out.println("No parameters"); } /* Overload the test for one integer parameter */ void test(int a) { System.out.println("a : " + a); } /* Overload the test for two integer parameters */ void test(int a, int b) { System.out.println("a is " + a + "\nb is " + b); } /* Overload the test for a double parameter */ double test(double a) { System.out.println("double a : " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; /* all the version of method test() */ ob.test(); ob.test(100); ob.test(100, 200); result = ob.test(123.25); System.out.println("Result of ob.test(123.25) is " + result); } }
When the above Java program is compile and executed, it will produce the following output:
As you can see, test() method is overloaded four times as follows:
- test() - takes no parameters
- test(int a) - takes one integer parameter
- test(int a, int b) - takes two integer parameters
- test(double a) - takes one double parameter
The fact that the fourth version of the test() also returns a value is of no consequence relative to overloading, since return types don't play a role in overload resolution.
Java Method Overloading Program
When an overloaded method is called, Java looks for a match between the arguments used to call the method and method's parameters. However, this match need not always be exact. In several cases, Java's automatic type conversions can act a role in overload resolution. For example, consider the following program :
/* Java Program Example - Java Method Overloading * Automatic type conversion apply to overloading */ class OverloadDemo { void test() { System.out.println("No parameters"); } /* overload test for the two integer parameters */ void test(int a, int b) { System.out.println("a is " + a + "\nb is " + b); } /* overload test for a double parameter */ void test(double a) { System.out.println("Inside test(double) a : " + a); } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); int i = 88; ob.test(); ob.test(100, 200); ob.test(i); // this will invoke test(double) ob.test(123.2); // this will invoke test(double) } }
When the above Java program is compile and executed, it will produce the following output:
As you can see, this version of the OverloadDemo does not define test(int). Therefore, when test() is called with an integer argument inside the Overload, no matching method is found. However, Java can automatically convert an integer into double, and this conversion can be used to resolve the call. Therefore, after test(int) had been defined, it would have been called rather. Java will employ its automatic type conversions only if no exact match is found.
« Previous Tutorial Next Tutorial »