Implementation of Java method overloading (changing the data types)
Codeaft.java
classArea{staticintrectangle(inta,intb){returna*b;}staticdoublerectangle(doublea,doubleb){returna*b;}}classCodeaft{publicstaticvoidmain(Stringargs[]){System.out.println("Area of Rectangle "+Area.rectangle(3.14,10.0));System.out.println("Area of Rectangle "+Area.rectangle(5,10));}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Area of Rectangle 31.400000000000002
Area of Rectangle 50
codeaft@codeaft:~$
Implementation of Java method overloading (changing the number of arguments)
Codeaft.java
classArea{staticintrectangle(inta,intb){returna*b;}staticintrectangle(inta){returna*a;}}classCodeaft{publicstaticvoidmain(Stringargs[]){//Every square is rectangleSystem.out.println("Area of Square "+Area.rectangle(5));System.out.println("Area of Rectangle "+Area.rectangle(5,10));}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Area of Square 25
Area of Rectangle 50
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.