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
Post a Comment