Method Over Loading – Java

Java Method:

 Method is a block of code which runs when called.

We can pass data into method using parameters.

Methods are used to perform certain actions, which are called as functions.

A method must be declared within a class. It is followed by parenthesis.

Java provides us with some pre-defined methods such as System.out.println(“”)

Example:

Public class Main {

Static void myMethod() {

System.out.println(“I just completed the code”);

}

Public Static void Main(String[] args){

myMethod;

}

}

// myMethod can also be called many times by just:

Public class Main {

·Static void myMethod() {

·System.out.println(“I just completed the code”);

}

Public Static void Main(String[] args){

  myMethod;

myMethod;

 myMethod;

myMethod;

}

}

What is method overloading?

It is the process of defining the same method multiple times with different names or parameters.

Example: int MyMethod(int x, int y);

Double Mymethod(int x);

Float MyMethod(int x, int y);




public class Main {
static int plusMethodInt (int x, int y){
return x + y;
}
static double plusMethodDouble (double x, double y){
return x + y;
}

public static void main (String[] args) {
int myNum1 = plusMethodInt(7, 7);
double myNum2 = plusMethodDouble(8.7, 8.9);
System.out.println("int: "+ myNum1);
System.out.println("double: "+ myNum2);
}
}

Scroll to top