0% found this document useful (0 votes)
14 views

Class 10 Method Overloading

Method overloading

Uploaded by

Rasool Shaik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Class 10 Method Overloading

Method overloading

Uploaded by

Rasool Shaik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Polymorphism: The process of performing different actions by same names.

There are
two types of polymorphism:

1. Method overloading
2. Method overriding

Method overloading:

1. It is a process of performing different actions by one name. It is possible by


defining multiple methods in same class with same name but parameters must be
different, we can differentiate parameters with respect to three cases.

a) Number of parameters

-> sum(int)
-> sum(int, int)
-> sum(int[])

Example Program:

class MathOperation{
/* return sum of only two integers */
public int sum(int num1, int num2){
return num1+num2;
}

/* return sum of only three integers */


public int sum(int num1, int num2, int num3){
return num1+num2+num3;
}

/* return sum of n number of integers */


public int sum(int array[]){
int result = 0;
for(int value : array){
result += value;
}
return result;
}
}
class Main{
public static void main(String[] args){
MathOperation op = new MathOperation();
System.out.println(op.sum(2, 2));
System.out.println(op.sum(2, 2, 2));
System.out.println(op.sum(new int[]{2, 2, 2, 2}));
}
}

Output:

4
6
8

b) Type of parameters

class MergeOperation{
public String merge(String firstName, String lastName){
return firstName+" "+lastName;
}
public StringBuilder merge(StringBuilder firstName, StringBuilder lastName){
String temp1 = new String(firstName);
String temp2 = new String(lastName);
StringBuilder result = new StringBuilder(merge(temp1, temp2));
return result;
}
}
class Main{
public static void main(String[] args){
MergeOperation op = new MergeOperation();
System.out.println(op.merge("Gandivdhaari", "Arjun"));
System.out.println(op.merge(new StringBuilder("Vijaydhanushdhaari"),
new StringBuilder("Karna")));
}
}

Output:

Gandivdhaari Arjun
Vijaydhanushdhaari Karna

c) Sequence of parameters:

class PrintArguments{
public void printArgs(int arg1, float arg2){
System.out.println("arg1: "+arg1+", arg2: "+arg2);
}
public void printArgs(float arg1, int arg2){
System.out.println("arg1: "+arg1+", arg2: "+arg2);
}
}
class Main{
public static void main(String[] args){
PrintArguments arg = new PrintArguments();
arg.printArgs(2, 5.4f);
arg.printArgs(3.4f, 9);
}
}

Output:

arg1: 2, arg2: 5.4


arg1: 3.4, arg2: 9

2. In method overloading compiler checks method signature only i.e whether all the
methods have same name and different parameter list. return type, access modifiers
doesn't require to be same. Method signature means only method name and parameter
list.

Example Program:

class MathOperation{
/* return int value */
public int getSum(int num1, int num2){
return num1+num2;
}
/* return float value */
public float getSum(float num1, float num2){
return num1+num2;
}

/* return double value */


public double getSum(double num1, double num2){
return num1+num2;
}
}
class Main{
public static void main(String[] args){
MathOperation op = new MathOperation();
System.out.println(op.getSum(2, 2));
System.out.println(op.getSum(2.5f, 2.5f));
System.out.println(op.getSum(2.9, 2.9));
}
}

Output:

4
5.0
5.8

3. As method overloading done in same class, it is possible to overload any method


like private, default, protected, public, static, final, synchronized and non-
synchronized.

Example Program:

class OverloadingTest{
/* private method */
private void method(int val){
System.out.println("Overloaded private method..!");
}
/* default method */
void method(long val){
System.out.println("Overloaded default method..!");
}
/* protected method */
protected void method(float val){
System.out.println("Overloaded protected method..!");
}
/* public method */
public void method(double val){
System.out.println("Overloaded double method..!");
}
/* static method */
static void method(char val){
System.out.println("Overloaded static method..!");
}
/* final method */
final void method(String val){
System.out.println("Overloaded final method..!");
}
/* synchronized method */
synchronized void method(boolean val){
System.out.println("Overloaded synchronized method..!");
}
public static void main(String[] args){
OverloadingTest test = new OverloadingTest();
test.method(10);
test.method(10l);
test.method(10.0f);
test.method(10.0);
test.method('K');
test.method("Karna Coding Tution");
test.method(true);
}
}

Output:

Overloaded private method..!


Overloaded default method..!
Overloaded protected method..!
Overloaded double method..!
Overloaded static method..!
Overloaded final method..!
Overloaded synchronized method..!

4. If two methods have same type parameters but one is primitive type and other one
is Object type then first priority always comes to primitive than object type. If
primitive is absent then Object gets priority.

Example Program-1: Two different method with same types one is primitive and other
one is object.

class PrimitiveObjectTest{
public static void printValue(int val){
System.out.println("Primitive Int Method..!");
}
public static void printValue(Integer val){
System.out.println("Integer Object Method..!");
}
public static void main(String[] args){
printValue(10);
printValue(new Integer(10));
printValue(null);
}
}

Output:

Primitive Int Method..!


Integer Object Method..!
Integer Object Method..!

Example Program-2: If primitive type is absent then Object gets priority.

class PrimitiveObjectTest{
public static void printValue(Integer val){
System.out.println("Integer Object Method..!");
}
public static void main(String[] args){
printValue(10);
printValue(new Integer(10));
printValue(null);
}
}

Output:

Integer Object Method..!


Integer Object Method..!
Integer Object Method..!

5. If two methods having object types where one has Parent type and other one has
Child type then if we pass common value then Child gets high/first priority.

Example Program:

class Kunti{

}
class Karna extends Kunti{

}
class ObjectPriorityTest{
public static void method(Kunti obj){
System.out.println("Kunti method executed..!");
}
public static void method(Karna obj){
System.out.println("Karna method executed..!");
}
public static void main(String[] args){
method(new Kunti());
method(new Karna());
/*null is default/common value to any object, in this case Karna is
Child of Kunti hence Karna type method matched*/
method(null);
}
}

Output:

Kunti method executed..!


Karna method executed..!
Karna method executed..!

6. If two methods having object types where two methods have independant Childs of
a Parent then if we pass a common value then two childs have same priority so it is
ambiguity problem.

Example Program:

class Kunti{

}
class Karna extends Kunti{

}
class Yudistir extends Kunti{

}
class ObjectPriorityTest{
public static void method(Kunti obj){
System.out.println("Kunti method executed..!");
}
public static void method(Karna obj){
System.out.println("Karna method executed..!");
}
public static void method(Yudistir obj){
System.out.println("Yudistir method executed..!");
}
public static void main(String[] args){
method(new Kunti());
method(new Karna());
method(new Yudistir());
/*null is default/common value to any object, in this case Karna and
Yudistir both are indepandant childs of Kunti hence two methods have same priority
then ambiguity problem */
method(null);
}
}

7. At the time of execution if there is no parameter of argument type then the


compiler won't raises error immediately. The argument will be promoted to next
level for a match and this process continues until all the promotions and still
there is no match then compiler raises error. This is applicable for both primitive
and referance types.

Case-1:

class AutoPromote{
public static void method(double val){
System.out.println("double argument method..!");
}
public static void method(float val){
System.out.println("float argument method..!");
}
public static void method(long val){
System.out.println("long argument method..!");
}
public static void method(int val){
System.out.println("int argument method..!");
}
public static void method(short val){
System.out.println("short argument method..!");
}
public static void method(byte val){
System.out.println("byte argument method..!");
}
public static void method(char val){
System.out.println("char argument method..!");
}
}
class Main{
public static void main(String[] args){
byte b = 10;
/*There is a match for byte argument*/
AutoPromote.method(b);

short s = 10;
/*There is a match for short argument*/
AutoPromote.method(s);

/*There is a match for int argument*/


AutoPromote.method(10);

/*There is a match for long argument*/


AutoPromote.method(10l);

/*There is a match for float argument*/


AutoPromote.method(10.7f);

/*There is a match for double argument*/


AutoPromote.method(10.3);

/*There is a match for char argument*/


AutoPromote.method('B');
}
}

Output:

byte argument method..!


short argument method..!
int argument method..!
long argument method..!
float argument method..!
double argument method..!
char argument method..!

Case-2:

class AutoPromote{
public static void method(double val){
System.out.println("double argument method..!");
}
public static void method(float val){
System.out.println("float argument method..!");
}
public static void method(long val){
System.out.println("long argument method..!");
}
public static void method(int val){
System.out.println("int argument method..!");
}
public static void method(short val){
System.out.println("short argument method..!");
}
public static void method(char val){
System.out.println("char argument method..!");
}
}
class Main{
public static void main(String[] args){
byte b = 10;
/*There is no match for byte, it will be promoted to short and there is
match for short argument*/
AutoPromote.method(b);

short s = 10;
/*There is a match for short argument*/
AutoPromote.method(s);

/*There is a match for int argument*/


AutoPromote.method(10);

/*There is a match for long argument*/


AutoPromote.method(10l);

/*There is a match for float argument*/


AutoPromote.method(10.7f);

/*There is a match for double argument*/


AutoPromote.method(10.3);

/*There is a match for char argument*/


AutoPromote.method('B');
}
}

Output:

short argument method..!


short argument method..!
int argument method..!
long argument method..!
float argument method..!
double argument method..!
char argument method..!

Case-3:

class AutoPromote{
public static void method(double val){
System.out.println("double argument method..!");
}
public static void method(float val){
System.out.println("float argument method..!");
}
public static void method(long val){
System.out.println("long argument method..!");
}
public static void method(int val){
System.out.println("int argument method..!");
}
public static void method(char val){
System.out.println("char argument method..!");
}
}
class Main{
public static void main(String[] args){
byte b = 10;
/*There is no match for byte, it will be promoted to short and there is
not match for short and it will be promoted to int so there is a match for int
argument*/
AutoPromote.method(b);

short s = 10;
/*There is no match for short argument, it will be promoted to next
level int and there is match for int argument*/
AutoPromote.method(s);

/*There is a match for int argument*/


AutoPromote.method(10);

/*There is a match for long argument*/


AutoPromote.method(10l);

/*There is a match for float argument*/


AutoPromote.method(10.7f);

/*There is a match for double argument*/


AutoPromote.method(10.3);

/*There is a match for char argument*/


AutoPromote.method('B');
}
}

Output:

int argument method..!


int argument method..!
int argument method..!
long argument method..!
float argument method..!
double argument method..!
char argument method..!

Case-4:

class AutoPromote{
public static void method(double val){
System.out.println("double argument method..!");
}
public static void method(float val){
System.out.println("float argument method..!");
}
public static void method(long val){
System.out.println("long argument method..!");
}
public static void method(char val){
System.out.println("char argument method..!");
}
}
class Main{
public static void main(String[] args){
byte b = 10;
/*There is no match for byte, it will be promoted to short and there is
not match for short and it will be promoted to int and there is no match for int so
it will promote to long and there is a match for long argument*/
AutoPromote.method(b);

short s = 10;
/*There is no match for short argument, it will be promoted to next
level int and there is no match for int and it will promote to long and there is a
match for long argument*/
AutoPromote.method(s);
/*There is no match for int, it will promote to long and there is a
match for long argument*/
AutoPromote.method(10);

/*There is a match for long argument*/


AutoPromote.method(10l);

/*There is a match for float argument*/


AutoPromote.method(10.7f);

/*There is a match for double argument*/


AutoPromote.method(10.3);

/*There is a match for char argument*/


AutoPromote.method('B');
}
}

Output:

long argument method..!


long argument method..!
long argument method..!
long argument method..!
float argument method..!
double argument method..!
char argument method..!

Case-4:

class AutoPromote{
public static void method(double val){
System.out.println("double argument method..!");
}
public static void method(float val){
System.out.println("float argument method..!");
}
public static void method(char val){
System.out.println("char argument method..!");
}
}
class Main{
public static void main(String[] args){
byte b = 10;
/*There is no match for byte, it will be promoted to short and there is
not match for short and it will be promoted to int and there is no match for int so
it will promote to long and there is no match for long and it will promote to float
and there is a match for float argument*/
AutoPromote.method(b);

short s = 10;
/*There is no match for short argument, it will be promoted to int and
there is no match for int so it will promote to long and there is no match for long
and it will promote to float and there is a match for float argument*/
AutoPromote.method(s);

/*There is no match for int, it will promote to long and there is no


match for long and it will promote to float and there is a match for float
argument*/
AutoPromote.method(10);

/*There is no match for long argument, it will promote to float and


there is a match for float argument*/
AutoPromote.method(10l);

/*There is a match for float argument*/


AutoPromote.method(10.7f);

/*There is a match for double argument*/


AutoPromote.method(10.3);

/*There is a match for char argument*/


AutoPromote.method('B');
}
}

Output:

float argument method..!


float argument method..!
float argument method..!
float argument method..!
float argument method..!
double argument method..!
char argument method..!

Case-5:

class AutoPromote{
public static void method(double val){
System.out.println("double argument method..!");
}
public static void method(char val){
System.out.println("char argument method..!");
}
}
class Main{
public static void main(String[] args){
byte b = 10;
/*There is no match for byte, it will be promoted to short and there is
not match for short and it will be promoted to int and there is no match for int so
it will promote to long and there is no match for long and it will promote to float
and there is no match for float and it will promote to double and there is a match
for double argument*/
AutoPromote.method(b);

short s = 10;
/*There is no match for short argument, it will be promoted to int and
there is no match for int so it will promote to long and there is no match for long
and it will promote to float and there is no match for float and it will promote to
double and there is a match for double argument*/
AutoPromote.method(s);

/*There is no match for int, it will promote to long and there is no


match for long and it will promote to float and there is no match for float and it
will promote to double and there is a match for double argument*/
AutoPromote.method(10);
/*There is no match for long argument, it will promote to float and
there is no match for float and it will promote to double and there is a match for
double argument*/
AutoPromote.method(10l);

/*There is no match for float, it will promote to double and there is a


match for double argument*/
AutoPromote.method(10.7f);

/*There is a match for double argument*/


AutoPromote.method(10.3);

/*There is a match for char argument*/


AutoPromote.method('B');
}
}

Output:

double argument method..!


double argument method..!
double argument method..!
double argument method..!
double argument method..!
double argument method..!
char argument method..!

Case-6:

class AutoPromote{
public static void method(char val){
System.out.println("char argument method..!");
}
}
class Main{
public static void main(String[] args){
byte b = 10;
/*There is no match for byte, it will be promoted to short and there is
not match for short and it will be promoted to int and there is no match for int so
it will promote to long and there is no match for long and it will promote to float
and there is no match for float and it will promote to double and there is a match
for double and there is no match, hence compiler raises error*/
AutoPromote.method(b);

short s = 10;
/*There is no match for short argument, it will be promoted to int and
there is no match for int so it will promote to long and there is no match for long
and it will promote to float and there is no match for float and it will promote to
double and there is no match for double argument, hence compiler raises error*/
AutoPromote.method(s);

/*There is no match for int, it will promote to long and there is no


match for long and it will promote to float and there is no match for float and it
will promote to double and there is no match for double argument, hence compiler
raises error*/
AutoPromote.method(10);

/*There is no match for long argument, it will promote to float and


there is no match for float and it will promote to double and there is no match for
double argument, hence compiler raises error*/
AutoPromote.method(10l);

/*There is no match for float, it will promote to double and there is


no match for double argument, hence compiler raises error*/
AutoPromote.method(10.7f);

/*There is no match for double argument, hence compiler raises error*/


AutoPromote.method(10.3);

/*There is a match for char argument*/


AutoPromote.method('B');
}
}

Case-7:

class AutoPromote{
public static void method(int val){
System.out.println("int argument method..!");
}
}
class Main{
public static void main(String[] args){
/*There is no match for char argument, it will promote to int and there
is a match for int argument*/
AutoPromote.method('B');
}
}

Output:

int argument method..!

8. In method overloading, priority comes to referance type but not for object. At
runtime, JVM takes care of method resolution based on referance type. Hence it is
also called as 'Compile time polymorphism/earling binding/static polymorphism'.

class Parent{
public int sum(int num1, int num2){
return num1+num2;
}
public int sum(int num1, int num2, int num3){
return num1+num2+num3;
}
}
class Child extends Parent{
public int sum(int num1, int num2, int num3, int num4){
return num1+num2+num3+num4;
}
}
class Main{
public static void main(String[] args){
Parent parent = new Child();
/* using parent referance we can access only Parent class specific
methods but not those in Child class*/
System.out.println(parent.sum(2, 3));
System.out.println(parent.sum(2, 3, 4));
/* The following statement gives us error*/
System.out.println(parent.sum(2, 3, 4, 5));
}
}

You might also like