class Calculator { int add(int... numbers) { int sum = 0; for (int num : numbers) { sum += num; } return sum; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 6)); System.out.println(calc.add(5, 6, 7)); System.out.println(calc.add(5, 6, 7, 8)); } }
I feel Java is unable to satisfy user requirements here.. I may return a value I may not return a value with same type and number of arguments. But Java doesn't support that.
class Calculator{ public int add(int n1,int n2){ return n1+n2; } public int add(int n1,int n2,int n3){ return n1+n2+n3; } public double add(double n1,int n2,int n3){ return n1+n2+n3; } } public class MethodOverloading { public static void main(String[] args){ Calculator obj= new Calculator();
Just simple as in parameter u should mention the values as it mention in the method...Java will take the crt parameter and complie the value polymorphism
Class can have same name methods, only requirement is they should have different parameters, name and type is important!
Don't make it harder by your complicated words
@@bestlearn6131 that's not hard at all he just gave simple definition 😃
@@bestlearn6131 knowing the meaning of those words is necessary to understand what a method overloading really does...
@@bestlearn6131he just explained 6min video in two sentences
class Calculator {
int add(int... numbers) {
int sum = 0;
for (int num : numbers) {
sum += num;
}
return sum;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 6));
System.out.println(calc.add(5, 6, 7));
System.out.println(calc.add(5, 6, 7, 8));
}
}
doesnt work this way
Best explanation sir
Thank you so much sir😊
I feel Java is unable to satisfy user requirements here.. I may return a value I may not return a value with same type and number of arguments. But Java doesn't support that.
Easy to understand and concise!
Great teaching skills
good explanation....👏
I think JavaScript handle's this issue quite interestingly with the help of rest parameter.
class Calculator{
public int add(int n1,int n2){
return n1+n2;
}
public int add(int n1,int n2,int n3){
return n1+n2+n3;
}
public double add(double n1,int n2,int n3){
return n1+n2+n3;
}
}
public class MethodOverloading {
public static void main(String[] args){
Calculator obj= new Calculator();
double r1=obj.add(4,8);
System.out.println(r1);
}
}
Sir class can be one type of function because in other language it's have same syntax somewhat
When we return double value, the variable in main class to which the value will be assigned should also be of the type double, right?
Yes........!!
Your output will be XY.00
Thank you sir 😊❤
I have a Question, Why are you using public keyword inside Calculator Class with various methods, i think its not needed sir .
Can you make a video on JavaDoc?
sir could you help how to give input into java like scanf in c
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = sc.nextInt();
No two methods with same name must have same parameter type and same number of parameters.
i know very out of subject thing
i kinda went back in nostalgia when he said got7 at 2:00
💚💚💚💚
Aghase spotted
Bhupendra jogi
genuine
nothing 🤣
Kuss marwa rha h english accent dekh apna
sir in this video u took same method name "add" then how could the class demo can understand which one need to take
Just simple as in parameter u should mention the values as it mention in the method...Java will take the crt parameter and complie the value polymorphism