Function overloading and return type

1.2

In C++ and Java, functions can not be overloaded if they differ only in the return type.

For example, the following program C++ and Java programs fail in compilation.

C++ Program

#include<iostream>
int foo() { 
    return 10; 
}

char foo() {  // compiler error; new declaration of foo()
    return 'a'; 
}

int main()
{
    char x = foo();
    getchar();
    return 0;
}

Java Program

// filename Main.java
public class Main {
    public int foo() {
        return 10;
    }
    public char foo() { // compiler error: foo() is already defined
        return 'a';
    }
    public static void main(String args[])
    { 
    }
}

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.2 Average Difficulty : 1.2/5.0
Based on 59 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.