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

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.


HTML Image tag, code...
How to code in HTML...
HTML text tag...
HTML text, how to display...
Container responsive styles...
HTML container tag...
HTML container...
Send forgot password by mail or message ...
PHP MySQL simple login system...
Check for duplicate username live or ema...
How to create simple search engine using...
Diseño de sitio web CSS Layout...
Diseño de caja flexible CSS3...
Botones en CSS...
ID y Clase, Diferencias entre Class e Id...
Esquina redondeada CSS...
Tamaño de caja css box-sizing: border-b...
Alineación CSS...
Altura y Anchura CSS...
Bordes CSS...