What is methods in java ?
-->A Method provides information about, and access to, a single method on a class or interface.
-->The reflected method may be a class method or an instance method (including an abstract method).
-->method is the block of code
->You can pass data, known as parameters, into a method.
-->Methods are used to perform certain actions, and they are also known as functions.
-->Why use methods? To reuse code: define the code once, and use it many times.
//class
public class Calculater{
//main method
public static void main (String[] args){
// create in Object
Calculater casio = new Calculater();
// calling method
casio.add();
}
// method
public void add (){
int i = 10;
int j = 5;
int result = i +j;
System.out.println(result);
}
}
What is method overloading?
Method overloading:
--> Method overloading in Java allows a class to have multiple methods with the same name but different argument lists.
--> It is a form of compile-time polymorphism, because the compiler determines which overloaded method to call based on the arguments passed.
ublic class Calculater{
public static void main (String[] args){
Calculater casio = new Calculater();
// casio.add();
casio.add(10,20);
casio.add(100,20);
casio.add(10,20,50);
}
//method overloading
//same method name different argument
public void add (int i , int j, int k ){
int result = i +j+k;
System.out.println(result);
}
public void add (int i , int j){
int result = i +j;
System.out.println(result);
}
}

Top comments (0)