Skip to content

Instantly share code, notes, and snippets.

@TheKidCoder
Created October 1, 2013 20:29
Show Gist options
  • Save TheKidCoder/6784600 to your computer and use it in GitHub Desktop.
Save TheKidCoder/6784600 to your computer and use it in GitHub Desktop.
Argument Polymorphism in Java.
class DemoOverload{
public int add(int x, int y){ //method 1
return x+y;
}
public int add(int x, int y, int z){ //method 2
return x+y+z;
}
public int add(double x, int y){ //method 3
return (int)x+y;
}
public int add(int x, double y){ //method 4
return x+(int)y;
}
}
class Test{
public static void main(String[] args){
DemoOverload demo=new DemoOverload();
System.out.println(demo.add(2,3)); //method 1 called
System.out.println(demo.add(2,3,4)); //method 2 called
System.out.println(demo.add(2,3.4)); //method 4 called
System.out.println(demo.add(2.5,3)); //method 3 called
}
}
@benjreinhart
Copy link

same with the main function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment