0% found this document useful (0 votes)
1 views4 pages

Core Java - OOP Concepts Method Overloading

The document discusses method overloading in Java, explaining that it allows multiple methods with the same name but different signatures. It highlights that overloading cannot be achieved by changing only the return type, and that the JVM looks for a specific main method signature when starting a program. Additionally, it covers type promotion in method overloading and provides examples of ambiguity in method calls.

Uploaded by

amitvani2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Core Java - OOP Concepts Method Overloading

The document discusses method overloading in Java, explaining that it allows multiple methods with the same name but different signatures. It highlights that overloading cannot be achieved by changing only the return type, and that the JVM looks for a specific main method signature when starting a program. Additionally, it covers type promotion in method overloading and provides examples of ambiguity in method calls.

Uploaded by

amitvani2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Core Java - OOP Concepts: Method Overloading

Interview Questions

72) What is method overloading?


Method overloading is the polymorphism technique that allows us to create
multiple methods with the same name but different signatures. Method
overloading in Java occurs when a class has multiple methods with the same
name but different parameter lists (number, type, or order of parameters). We
can achieve method overloading in the following two ways:

○​ Different number of arguments


○​ Different data types of arguments

Method overloading increases the readability of the program. Method


overloading is performed to figure out the program quickly.

To read more: Method Overloading in Java

73) Why is method overloading not possible by changing


the return type in Java?
In Java, method overloading is not possible by changing the return type of the
program to avoid ambiguity. The method name and the parameter types
determine the method signature. Changing only the return type does not
provide enough information for the compiler to distinguish between
overloaded methods.

​ class Adder{
​ static int add(int a,int b){return a+b;}
​ static double add(int a,int b){return a+b;}
​ }
​ class Main {
​ public static void main(String[] args){
​ System.out.println(Adder.add(11,11));//ambiguity
​ }}
Output:
Compile Time Error: method add(int, int) is already defined in class
Adder

74) Can we overload the methods by making them static?


No, we cannot overload the methods by just applying the static keyword to
them (number of parameters and types are the same). Consider the following
example.

​ public class Animal


​ {
​ void consume(int a)
​ {
​ System.out.println(a+" consumed!!");
​ }
​ static void consume(int a)
​ {
​ System.out.println("consumed static "+a);
​ }
​ public static void main (String args[])
​ {
​ Animal a = new Animal();
​ a.consume(10);
​ Animal.consume(20);
​ }
​ }
Output

Animal.java:7: error: method consume(int) is already defined in class


Animal
static void consume(int a)
^
Animal.java:15: error: non-static method consume(int) cannot be
referenced from a static context
Animal.consume(20);
^
2 errors

75) Can we overload the main() method?


Yes, we can have any number of main() methods in a Java program by using
method overloading. However, the JVM looks for the standard public static
void main(String[] args) signature when starting a Java program. Overloaded
main() methods can be called from within the standard main() method, but the
program execution begins from the standard main() method.

To read more: Can we overload the main() method in Java

76) What is method overloading with type promotion?


By type promotion is method overloading, we mean that one data type can be
promoted to another implicitly if no exact matching is found.

Java Method Overloading with Type Promotion

As displayed in the above diagram, the byte can be promoted to short, int,
long, float, or double. The short datatype can be promoted to int, long, float, or
double. The char datatype can be promoted to int, long, float, or double, and so
on. Consider the following example.

​ class Main {
​ void sum(int a,long b){System.out.println(a+b);}
​ void sum(int a,int b,int c){System.out.println(a+b+c);}
​ public static void main(String args[]){
​ Main obj=new Main();
​ obj.sum(20,20);//now second int literal will be promoted to long
​ obj.sum(20,20,20);
​ }
​ }
Output

40
60

77) What is the output of the following Java program?


​ class Main {
​ void sum(int a,long b){System.out.println("a method invoked");}
​ void sum(long a,int b){System.out.println("b method invoked");}
​ public static void main(String args[]){
​ Main obj = new Main();
​ obj.sum(20,20);//now ambiguity
​ }
​ }
Output

Main.java:7: error: reference to sum is ambiguous


obj.sum(20,20);//now ambiguity
^
both method sum(int,long) in Main
and method sum(long,int) in Main match
1 error

Explanation

There are two methods defined with the same name, i.e., sum. The first
method accepts the integer and long types, whereas the second method
accepts long and the integer types. The parameters passed are a = 20, b = 20.
We cannot tell which method will be called, as there is no clear differentiation
mentioned between integer literal and long literal. This is the case of
ambiguity. Therefore, the compiler will throw an error.

You might also like