AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Java Overloading Method


Java Tutorial » Java Overloading Method

Method overloading allows multiple methods that can have the same name with different signatures where the signature can differ by the number of input parameters:


Example:
int my_Method(int a)
float my_Method(float a)
double my_Method(double a, double b)

Java program that can demonstrate how is working overloading method

Example: have two methods that add numbers of different type:
public class Main {

// Overloaded sum(). This sum takes two int parameters
static int plus_MethodInt(int a, int b)
{
return a + b;
}

// Overloaded sum(). This sum takes two double parameters
static double plus_MethodDouble(double a, double b)
{
return a + b;
}

// Overloaded sum(). This sum takes three int parameters
static int sum(int a, int b, int c) {
return (a + b + c);
}

public static void main(String[] args)
{
int my_Num1 = plus_MethodInt(12, 15);
double my_Num2 = plus_MethodDouble(5.3, 7.26);
int my_Num3 = sum(4, 26, 41);
System.out.println("int: " + my_Num1);
System.out.println("double: " + my_Num2);
System.out.println("int: " + my_Num3);
}
}
Output:
int: 27
double: 12.559999999999999
int: 71


Can we overload methods on return type? example


We cannot overload by return type. This behavior is same in C++. Refer this for details

Example
public class Main {
public int yaa() { return 24; }

// compiler error: yaa() is already defined
public char yaa() { return 'x'; }

public static void main(String args[])
{
}
}


Overloading methods on return type are possible in cases where the data type of the function being called is explicitly specified

Example
// Java program to demonstrate the working of method
// overloading in static methods
public class Main {

public static int yaa(int x) { return 24; }
public static char yaa(int x, int y) { return 'x'; }

public static void main(String args[])
{
System.out.println(yaa(1));
System.out.println(yaa(1, 2));
}
}
Output:
24
x


java overloading methods alternative, different return typs, inheritance, equals, overriding final method java

java overloading methods, operators, method, examples, vs overriding, return type, methods alternative, and overriding, constructor
Java Overloading Methods - java

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...