



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
these are general examples for method overloading
Typology: Exercises
1 / 7
This page cannot be seen from the preview
Don't miss anything!




//methods inside Main class class Methods { public static void main(String []args) { System.out.println("\t\t Welcome to Main"); callFirst(); System.out.println("\t\t Welcome Back to Main"); }
static void callFirst() { System.out.println("Welcome to First "); callSecond();
} static void callSecond() { System.out.println("Welcome to Second "); callThird();
static void callThird() { System.out.println("Welcome to Third");
}
//method with arguments : class MethodsArgs { public static void main(String []args) { int n1,n2;
n1=Integer.parseInt(args[0]); n2=Integer.parseInt(args[1]);
int res; res=getResult(n1,n2);
System.out.println("Result of " + n1 + " + " + n2 + " = " + res); }
//method with arguments and return value static int getResult(int n1,int n2) { return(n1+n2); } }
//Method overloading
//VarArgs concept
class VarArgs { public static void main(String []args) { printSum(1); printSum(1,2); printSum(1,2,3); printSum(1,2,3,4); printSum(1,2,3,4,5,6); } static void printSum(int ... list) /*var args concept */ { System.out.print("Sum of " + list.length + " Nos { "); int sum=0; for(int i : list) { System.out.print(i + " + "); sum+=i; } System.out.println("\b\b = " + sum + " ] "); } }
//concept of recursion class Recursion { public static void main(String []args) { int no; no=Integer.parseInt(args[0]);
System.out.println("Factorial of " + no + " is " + getFact(no)); } static int getFact(int x) /* recursion concept */ { if(x==1) return(1); else return(x * getFact(x-1)); } }
/* nesting of methods */
class NestedMethods { public static void main(String []args)
return("First"); else if(avg>60) return("Second"); else if(avg>50) return("Third"); else return("Fail"); } }