program using method and method overloading in java
Method describe behavior of an object. A method is a collection of statements that are group together to perform an operation.
There are three step to
define Method:
1) Declaration--> return_type
method_name()
2) Define --> {
}
3) Call --> call with object
Syntax:
return_type method_name(){
// body of method
}
Declaration of method:
void java_pivot(){
//body of method
System.out.println(“ I am java pivot method”);
}
//program to add two no. using method
public class Methodadd {
void add(int a,int b){
int c= a+b;
System.out.println(c);
}
public static void main(String[] args) {
Methodadd ad = new Methodadd();
ad.add(10, 12);
}
}
Output:
Program using
method overloading
Declaration of method overloading
Method name will be same but changes arguments like
void java_pivot(){
//body of method
System.out.println(“ I am java pivot method”);
}
void java_pivot(int a,
int b){
//body of method
}
// Program using method overloading
package methodaddmul;
class calculate{
void add(int a,int b)
{
System.out.println(a+b);
}
void add(float a, int b)
{
System.out.println(a+b);
}
void minus(float a, int b)
{
System.out.println(a-b);
}
void minus(float a, float b)
{
System.out.println(a-b);
}
void mul(float a, int )
{
System.out.println(a*b);
}
void mul(int a, int b, int
c)
{
System.out.println(a*b*c);
}
}
public class MethodAddmul {
public static void main(String[] args) {
calculate cl =new calculate();
System.out.println("add
of two integer no. is:");
cl.add(5,
05);
System.out.println("add
of two float and integer no. is:");
cl.add(5.3f,
6);
System.out.println("Substract of two
float and integer no. is:");
cl.minus(4.4f,
5);
System.out.println("Substract
of two float no. is:");
cl.minus(4.5f,
3.5f);
System.out.println("Multiply
of two float and integer no. is:");
cl.mul(5.5f, 3);
System.out.println("Multiply
of three integer no. is:");
cl.mul(2,
3,4);
}
}
0 comments: