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

Methods

Method overloading allows multiple methods to have the same name but different parameters. In C++, a Calculator class demonstrates method overloading by defining multiple "add" methods that take different parameter types, allowing integers, floats, and doubles to be added. This avoids needing separate method names and allows the same call "calc.add()" to work for any data type. However, method overloading is not polymorphism since different underlying methods are called based on parameters rather than runtime types.

Uploaded by

Suma Ph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Methods

Method overloading allows multiple methods to have the same name but different parameters. In C++, a Calculator class demonstrates method overloading by defining multiple "add" methods that take different parameter types, allowing integers, floats, and doubles to be added. This avoids needing separate method names and allows the same call "calc.add()" to work for any data type. However, method overloading is not polymorphism since different underlying methods are called based on parameters rather than runtime types.

Uploaded by

Suma Ph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Value and reference type assignment

4000

4000
Dog d1=new Dog(); d1
int a=4000 4000
int b; Dog d2;

b=a d2=d1 d2
4000
Value type assignment Reference type assignment
Object

has part does part


5th Rule : Every object has something and does
something . Has part is nothing but properties and
does part is nothing but behaviors .

Dog

has part does part

name void eat()


colour void sleep()
cost void bark()
datatypes methods
Types of Methods
There are 4 types of methods as listed below,
1) no input ,no output
2) Input but no output
3) no input but output
4) input , output
Output 30
1) no input ,no output
OS

class LaunchCalculator{
p s v m(String args[]){
Calculator calc=new Calculator();
void add(){
calc.add(); } a=10;
} b=20;
class Calculator{ c=a+b;
int a,b,c; S.o.p(c);
}
void add(){
4000
a=10;
p s v m(String args[]) a 0 10
b=20; {
c=a+b; Calculator calc=new Calculator(); b 0 20
calc
System.out.println(c); calc.add(); c 0 30
4000
}
}
}
Stack Heap
output name(input)
{
activity
}

returntype name(parameters)
{
Body of the method
}
Output 30
2) input but no output
OS

class LaunchCalculator{
p s v m(String args[]){
Calculator calc=new Calculator();
void add(int x,int y){
int a=10,b=20; c=x+y;
calc.add(a,b); } S.o.p(c);
} } x 10 y 20
class Calculator{
int c;
void add( int x,int y){ 4000
c=x+y; p s v m(String args[])
{
System.out.println(c); Calculator calc=new Calculator();
} int a=10,b=20; calc
calc.add(a,b); 4000 c 0 30
}
} a 10 b 20
Stack Heap
Output 30
3) no input but output
OS

class LaunchCalculator{
p s v m(String args[]){
Calculator calc=new Calculator(); int add(){
int res=calc.add(); a=10;
S.o.p(res);} b=20;
}class Calculator{ c=a+b;
return c;
int a,b,c;
}
int add(){
4000
a=10;
p s v m(String args[]) a 0 10
30 b=20; {
c=a+b; Calculator calc=new Calculator(); b 0 20
calc
return c; int res=calc.add(); c 0 30
4000
Sop(res);
} } res 30
}
Stack Heap
Output 30
4) Input output
OS

class LaunchCalculator{
p s v m(String args[]){
Calculator calc=new Calculator();
int add(int x,int y){
int a=10,b=20; c=x+y;
Int res=calc.add(a,b); return c;
Sop(res);} } x 10 y 20
}
class Calculator{ 4000
int c; p s v m(String args[])
30 int add( int x,int y){ { res 30
c=x+y; Calculator calc=new Calculator();
int a=10,b=20; calc
return c; Int res=calc.add(a,b); 4000 c 0 30
} } a 10 b 20
}
Stack Heap
Method Overloading
C
int add(int x,int y){ float add(int x, float y){
return x+y; return x+y;
} }

ERROR
In C programming language , to add numbers we
cannot have two methods with same name it
leads to error as shown above.
Method Overloading
C double add6(int x, float y, double
int add1(int x,int y){ z){
return x+y; return x+y+z;
double add11(float x, double
} }
float z){
float add2(int x, float y){ float add7(int x, float y, float return x+y+z;
return x+y; z){ }
} return x+y+z; float add12(float x, int y){
} return x+y;
float add3(float x,float y){ float add8(float x, float y, float z){ }
return x+y; return x+y+z;
} }

double add4(int x, double y){ double add9(double x, double y, double z){


return x+y; return x+y+z;
} }
int add5(int x, int y, int z){
return x+y+z; double add10(double x, float y, float z){
} return x+y+z;
}
class LaunchCalculator
{
Public static void main(String args[])
{
int p=10,q=20,r=30;
float m=10.5f,n=20.5f,o=30.5f;
double a=10.5,b=20.5,c=30.5;
//to add two integers=?????
//to add all three float numbers=?????
//to add all one int one float=?????

As we have used different names for different


methods to add different types of numbers . It is
difficult for a user to remember respective
method name.
C++
Method Overloading
class Calculator{
double add(int x, float y, double z){
int add(int x,int y){ return x+y+z;
return x+y; }
double add(float x, double y,
} float z){
float add(int x, float y){ float add(int x, float y, float z){ return x+y+z;
return x+y; return x+y+z; }
} } float add(float x, int y){
return x+y;
float add(float x,float y){ float add(float x, float y, float z){ }
return x+y; return x+y+z;
} } }

double add(int x, double y){ double add(double x, double y, double z){


return x+y; return x+y+z;
} }
int add(int x, int y, int z){
return x+y+z; double add(double x, float y, float z){
} return x+y+z;
}
class LaunchCalculator
{
Public static void main(String args[])
{
int p=10,q=20,r=30;
float m=10.5f,n=20.5f,o=30.5f;
double a=10.5,b=20.5,c=30.5;
//to add two integers
calc.add(p,q)
//to add all three float numbers=?????
calc.add(m,n,o);
//to add all one int one float=?????
calc.add(p,m)
}
}
For adding any type of numbers the method
name is add() hence the person who will code
main method will think that it is VIRTUAL
POLYMORPHISM.
VIRTUAL POLYMORPHISM.
false 1:M

The person who will be coding main method will


come to conclusion that one single method is
performing different operations but it is not true
in reality . Different methods for different
operations will be present.
• Method Overloading is a process of having
multiple methods with same name , same
number of parameters , same type of
parameters however the order of occurrence
of type of parameters should be different.
Prg 1)class Calculator class Calculator
{ {
void add(int x,float y)
P s v m(String args[])
{
float c=x+y; {
} Calculator calc=new
float add(int x,float y) Calculator();
{ Calc.add(10,20.5f);
float c=x+y;
return c; }
} }
} Output error:above program results in an error as this program (prg 1) results In
an error stating that already method add(int , float ) exists.
Above is the error because return type doesn’t play any role in method
overloading.
Prg 2)class Calculator class Calculator
{ {
void add(int x,float y)
{ P s v m(String args[])
float c=x+y; {
System.out.println(c);
}
Calculator calc=new
float add(int x,float y,double z) Calculator();
{ Calc.add(10,20.5f);
float c=x+y;
return c; }
} }
}
Output will be30.5 because of first method , not because of second method as it
was expecting 3 parameters which was not given while calling it.
Method overloading with numeric type promotion

Prg 3)class Calculator class Calculator


{ {
void add(int x,float y)
{ P s v m(String args[])
float c=x+y; {
System.out.println(c);
}
Calculator calc=new
double add(int x,float y,double z) Calculator();
{ Calc.add(10,20);
double c=x+y+z;
return c; }
} }
}
Output will be30.0 because of first method ,as it can store 20 in y as 20.0.
Prg 4)class Calculator class Calculator
{ {
void add(int x,float y)
{ P s v m(String args[])
float c=x+y; {
System.out.println(c);
}
Calculator calc=new
float add(float x,int y) Calculator();
{ Calc.add(10,20);
float c=x+y;
return c; }
} }
}
Output will be an error because if multiple methods are promoting numeric type
promotion it leads to ambiguity.

You might also like