Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.
Today we are learning How method overloading is implement in java.
package com.ritesh;
import java.util.Scanner;
public class function_overloading { //method number 1
public static int calc(int a){
a=10;
System.out.println(a);
return a;
}
public static int calc(int x,int y){ //method number 2
System.out.println("Enter the value of x");
Scanner sc=new Scanner(System.in);
byte n1=sc.nextByte();
System.out.println("Enter the value of y");
byte n2=sc.nextByte();
return n1+n2;
}
public static void calc(){ //method number 3
String [] name=new String[5];
name[0]="Ram";
name[1]="Shyam";
name[2]="Hari";
name[3]="Sita";
name[4]="Gita";
byte j=0 ;//array index start with zero to print all number on the array you should start the loop form zero
while(j<name.length){
System.out.println(name[j]);
j++;
}
}
public static void main(String[] args) {
calc(10); //calling method number 1
//calc(10,20);//calling method number 2
//to print the value of method number 2
System.out.println(calc(10,20));
calc();//calling method number 3
}
}
Comments
Post a Comment