Method Over loading :
Definition:
Writing multiple methods with the same name by passing different number of
arguments/parameters.
Writing multiple methods with the
same name by passing different data types.
Writing multiple methods with the
same name by changing the order of parameters or by changing the position of
data type.
ex1: // Writing same method name by passing diff no
of args (or) parameters - MOL
package OOPSBasics;
public class
MethodOveLoadingBasics1
{
//
Define M1 with no args/ parameters
// /// Instance Method -- (with out static k/w -
if we define method - NonStatic Method )
public
void M1()
{
System.out.println("calling M1 with No arguments (or) No
Parameters");
}
// // Define M1 with 1 int arg/
parameter
public
void M1(int a)// passing 1 arg/
para // Instance Method or Non static
Method
{
System.out.println("calling M1 with 1 argument (or) Parameter");
}
// // Define M1 with 2 int args/
parameters
public
void M1(int a, int b)// 2 parameters /
args
{
System.out.println("calling M1 with 2 arguments (or) Parameter");
}
public
static void main(String[] args) {
//
Method Over loading:
//
Create obj for class -MethodOveLoadingBasics1 with mObj1
MethodOveLoadingBasics1
mol = new MethodOveLoadingBasics1();
// call M1()
mol.M1();
//
call M1() - pass 10 value
mol.M1(10);
//
HW Call M1 with 2 args
}
}
o/p:
calling M1 with No arguments (or) No Parameters
calling M1 with 1 argument (or) Parameter
HW Define M2() with 1 float arg and Define M2() with 2 float args and Define
M2() with 3 float args, call all the
methods one by one ?
HW Define M3() with 1 char arg, 2 char args, 3
char args, call all the methods one by
one ?
HW Define M4() with 1 String arg, 2 String args,
3 String args, call all the methods one
by one ?
//2. Writing the same method
by passing diff data types
package OOPSBasics;
public class
MethodOveLoadingBasics2
{
//
Define M1 with 1 int arg
public
void M1(int a)// M1 with 1 args / Parameter
{
System.out.println("M1
Method with 1 int arg");
}
// Define M1 with 1 float arg
public
void M1(float a) // Method with 1
arg of type float
{
System.out.println("M1
Method with 1 arg of type float");
}
// Define M1 with 1 char arg
public
void M1(char ch) // 1 arg of char type
{
System.out.println("M1
Method with 1 arg of char type");
}
public
static void main(String[] args)
{
//create
obj for class- MethodOveLoadingBasics2
MethodOveLoadingBasics2
mol2 = new MethodOveLoadingBasics2();
//
call M1() with 1 int value
mol2.M1(10);
//
Call M1() with float val
mol2.M1(2.45f);
//
HW Call M1() with char val
}
}
o.p:
M1 Method with 1 int arg
M1 Method with 1 arg of type float
HW Define M2 () with 1 String arg
and Define M2() with 1 boolean arg and call all methods ?
//3. Writing the same method by
changing the position of data type
package OOPSBasics;
public class
MethodOveLoadingBasics3
{
//
Define M1() with 1 int arg and 1 char arg
public
void M1(int a, char ch)
{
System.out.println("Method
-2 args 1st arg is of int, 2nd arg is of type char ");
}
// define M1() with 1 char arg
and 1 int arg
public
void M1(char ch, int a) // // 2 args, 1st arg is of char, 2nd arg is of type
int
{
System.out.println("Method
-2 args 1st arg is of char, 2nd arg is of type int ");
}
public
static void main(String[] args)
{
//create
obj for class-
//HW call all the 2 methods
}
}
ex3:Define add()
with MOL
package OOPSBasics;
public class
MethodOveLoadingBasics2
{
//
Define add() with no args -
public
void add()// non static or instance
Method
{
System.out.println("Calling
add () with no args");
}
//
Define add() with 1 int arg
public
void add(int a)
{ //
10 . s0 a =10
System.out.println("Calling
add () with 1 args");
}
// // Define add() with 2 int args
public
void add(int a, int b)
{ // 10 20
int
res = a+b;
// 10 + 20
// 30
// res = 30
System.out.println("Calling
add () with 2 args. Res= "+ res);
//
30
}
// // Define add() with 3 int args
public
void add(int a, int b, int c)
{ // 10 20
30
int
res = a+b +c;
// 10 + 20 + 30
// 60
// res = 60
System.out.println("Calling
add () with 3 args. Res= "+ res);
//
60
}
public
static void main(String[] args) {
//
call non static or instance method -- 1st we have to create obj for class- MethodOveLoadingBasics2
MethodOveLoadingBasics2
mol2 = new MethodOveLoadingBasics2();
//
call add() with 1 args
mol2.add(10);
//
call add() with 2 args
mol2.add(10,20);
//
call add() with 3 args
mol2.add(10,
20, 30);
}
}
o/p:
Calling add () with 1 args
Calling add () with 2 args. Res=
30
Calling add () with 3 args. Res=
60
HW WAP to over load Multiplication method with 2
int number, 3 int numbers and call all
methods?
mul(2,3)
mul(2,3,4)
HW WAP to over load Multiplication method with 2
float numbers, 3 float numbers and call all methods??
mul(1.0f,2.0,3.0f);
Ex4:
Duplicate Methods are Not Allowed with the Same Signature
- Definition: In Java, a method's
signature is defined by its name and the parameter types. When two methods
have the same name and parameter types, they are considered to have the
same signature.
- Restriction: Java does not allow two
methods with the same signature in the same class because the compiler
cannot differentiate between them based on the method name and parameter
types alone.
package OOPSBasics;
public class
MethodOveLoadingBasics1
{
//
Define M1() // No args / no Parameters
public
void M1()
{
System.out.println("M1
Method with no args");
}
//
can we write same method and same data type? No
//
define method M1() // no args
// public void M1() // Error
Duplicate method M1() in type MethodOveLoadingBasics1
// {
// System.out.println("M1
Method with no args");
// }
//
No args / no Parameters //CE: Duplicate
method M1() in type MOLBasics1
//
Define M1() with 1 int arg
public
void M1(int a) //// M1() with 1 arg /
parameter
{
System.out.println("M1
Method with 1 int arg");
}
//
Define above method again
// public void M1(int a) ////
Error M1() with 1 arg / parameter
// {
// System.out.println("M1
Method with 1 int arg");
// }
public
static void main(String[] args) {
System.out.println("in
main ");
}
}
FAQ What is Method Over loading ?
**********************************************
**********************************************
Write the o/p for below program?
package OOPSBasics;
public class
MethodOveLoadingBasics3
{
static
void M1(char ch, int a) // 2 args 1st arg is of char, 2nd arg is of type int
{ // 'A' 10
so ch = 'A' , a = 10
int
res= ch + a;
// 'A'
+ 10
// 65 + 10 =
// 75
// res=
75
System.out.println("res="+
res);
// 75
System.out.println("Method
-2 args 1st arg is of char, 2nd arg is of type int ");
}
static
void M1(int a, char ch)// 2 args
{ // 2 'A' so a= 2 , ch ='A'
int
res= ch + a;
// 'A' + 2
// 65 + 2
// 67
// res=
67
System.out.println("res="+
res);
// 67
System.out.println("Method
-2 args 1st arg is of int, 2nd arg is of type char ");
}
public
static void main(String[] args) //
main() -static method
{
System.out.println("in
main ");
//
call M1(char ch, int a)
M1('A',10);
//
call static void M1(int a, char ch)/
M1(2,'A');
}
}
o/p:
in main
res=75
Method -2 args 1st arg is of
char, 2nd arg is of type int
res=67
Method -2 args 1st arg is of int,
2nd arg is of type char
**************************************************
2-Apr-2024
**************************************************
Write o/p for below program?
--------------------------------------
package OOPSBasics;
public class MOLBasics3
{
static
void M1(String s, int a)// static method
{ // Ram 10
so s ="Ram" and a =10
// int res=
s + a; // errorType mismatch: cannot convert from String to int
// "Ram" + 10
// "Ram10" o/p is in the form of String .
//so
left side, we must declare string variable but not int var
String
res1= s+ a;
// "Ram" + 10 --> concatenation - joining
// "Ram10"
// res1 = "Ram10"
System.out.println("res="+
res1);
// Ram10
}
public
static void main(String[] args)
{
System.out.println("in
main ");
M1("Ram",10);
}
}
o/p:
in main
res=Ram10
FAQ Can we over load main()?
package OOPSBasics;
public class
MethodOveLoadingBasics2
{
//
FAQ Can we over load main()? yes
void
main() // static or non static method or
Instance
{
System.out.println("Calling
main() - o args");
}
void
main(int a) // passing 1 arg
{
System.out.println("Calling
main() - 1 args- int");
}
void
main(char a)
{
System.out.println("Calling
main() - 1 args - char."+ a);
}
public
static void main(String [] args) {
//
Create obj for class -MethodOveLoadingBasics2
MethodOveLoadingBasics2 mol2 = new MethodOveLoadingBasics2 ();
//
call main() with no args
mol2.main();
//
call main() with 1 int args
mol2.main(10);
//
call main() with 1 char args
mol2.main('A');
//
call main() with String "Ramu" ?
// mol2.main("Ramu");//
Error // The method main(int) in
the type MethodOveLoadingBasics2 is not applicable for the arguments (String)
// error as we did not define / write any method
with String args/ parameter
//
call main() with double value 10.3
// mol2.main(2.45f);// error
// mol2.main(2.45d);// error
// error as we dont have main( double d) or
float arg
String
sarr [] = {"Ram", "sita","Lakshman"};
mol2.main(sarr);//
Exception in thread "main" java.lang.StackOverflowError
}
}
Note:
press Ctrl + A, and
Ctrl + I -- Indentation (or)
proper allignment of code
ex: if we dont have main() with string array , Can we run the program?
package OOPSBasics;
public class
MethodOveLoadingBasics2
{
// public static void main(String[]
args) // JVM always looks in current class main() which starts with public
// static void
and method with args String array
only -- Then JVM runs proagram
// if main (String [] arr) is not there,
JVM will not run the program
// We cannot see run as > Java application option
public
static void main(String[] sarr)
// public static void main(int[] iArr)
// public static void main(String s)
{
//
System.out.println("in
Main ");
}
}
Can we have 2 main(String args) ?
No.
package OOPSBasics;
public class
MethodOveLoadingBasics2
{
public
static void main(String[] sarr) // Errro :Duplicate method main(String[]) in
type MethodOveLoadingBasics2
{
}
public
static void main(String[] sarr)
{
//
System.out.println("in
Main ");
}
}
Can we pass diff array name in main()
should I pass array name i.e args in main()?
we can pass any diff array name -
other than args i.e sarr, arr
package OOPsBasics1;
public class
MethodOveLoadingBasics2 {
//
Note : we dont need to pass args
only -- we cann give / pass differ array
name i.e arr, sArr
// public static void main(String[] args) //
args - array variable anme -
public
static void main(String[] arr)
{
// m2.main("Ramu");
//
The method main(int) in the type MethodOveLoadingBasics2 is not applicable for
the arguments (String)
// error as we did not define / write any method
with String args/ parameter
// m2.main(10.3);// error as we dont main( double d)
}
}
Note:
// int main(int a) // changing return type of method -- does not come under MOL
// {
// System.out.println("Calling main() -
1 args- int");
// }
In Java, changing only the return type of a
method while keeping the same method name and parameter types does not
constitute method overloading.
Method overloading requires
methods to have different parameter lists, either in the number of parameters,
their types, or both. The return type alone cannot distinguish methods because
the method signature does not include the return type. Thus, having two methods
with the same name and parameter list but different return types will result in
a compilation error.
No comments:
Post a Comment