KnowledgeBoat Logo
|

Computer Applications

How does the compiler interpret more than one definitions having same name ? What steps does it follow to distinguish these ?

User Defined Methods

6 Likes

Answer

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:

  1. 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.
  2. 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) {...} and double 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) and double square (double d)
  3. 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.

Answered By

4 Likes


Related Questions