Method signatures (II)
4 March 2008Do read Method signatures (I) before this one since it is a continuation to it.
Answering the raised question: Some of you might think it is ok to have methods with same names, same no of parameters and all the variables have same datatype but with different since JRE will know which method to call depending on the variable that will get the value returned by the method.
Some of you might this that it is a run time error.
The answer is: it’s a compile time error. Let me give you an example:
public int getValue(int a) { System.out.println("I m in int getValue(int)"); return 1; } public float getValue(int a) { System.out.println("I m in float getValue(int)"); return 1.5f; }
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem
Actually, Java compiler treats both method same and duplicate method declaration is not allowed.
I hope I was able to provide interesting example.
Happy coding.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Method signatures (II)