Created
October 1, 2013 20:29
-
-
Save TheKidCoder/6784600 to your computer and use it in GitHub Desktop.
Argument Polymorphism in Java.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
same with the main function