ICSE Class 10 Computer Applications
Question 39 of 43
User-Defined Methods — Question 39
Back to all questions 39
Question Question 37
How does the compiler interpret more than one definitions having same name ? What steps does it follow to distinguish these ?
The compiler interprets more than one definitions having same name by matching the signature in the function definitions with the arguments given in the method call statement.
When a function name is declared more than once in a program, the compiler will interpret the second and subsequent declarations as follows:
- If the signatures of subsequent functions match the previous function's, then the second is treated as a re-declaration of the first and is flagged at compile time as an error.
- If the signatures of the two functions match exactly but the return types differ, the second declaration is treated as an erroneous re-declaration of the first and is flagged at compile time as an error. For example:
float square(float f) {...}anddouble square(float x)will be treated as an error.
Functions with the same signature and same name but different return types are not allowed in Java. We can have different return types, but only if the signatures are also different :float square (float f)anddouble square (double d) - If the signatures of the two functions differ in either the number or type of their arguments, the two functions are considered to be overloaded.