Java method overloading error with same return type? -


while executing basic method overloading program consistantly getting following error:

sh-4.3$ javac helloworld.java
helloworld.java:10: error: method show() defined in class helloworld
static void show()
helloworld.java:25: error: method show(int,int) defined in class helloworld
static void show(int a,int b)
2 errors

the code program follows

    public class helloworld{        static int show()          {              int c = 5+10;              system.out.println("hello");              return c;          }         static void show()          {              int c = 5+10;              system.out.println("void"+c);          }          static int show(int a,int b)          {              int c = a+b;              system.out.println("hello");              return c;          }          static void show(int a,int b)          {              int c = a+b;              system.out.println("hello void args"+c);          }      public static void main(string []args){     int a=5,b=5;         int c=show();         system.out.println("hello"+c);         show();         c= show(a,b);         system.out.println("hello"+c);         show(a,b);       } } 

a simple rule exists signature of methods in java:

method signature includes name of method + input parameter(s).

so return type of method not included in method signature.

on other hand overloading means:

having 2 or more mathods in class exact same name different parameter types or different number of parameters or both.

compiler not indicate return types of methods, can not understand difference between 2 show methods 2 int parameters. same mistake happens 2 show methods without parameters.

hope clarification enlightening.

good luck.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -