Confusing Java, Overloading : A Java Interview Question

Serdar A.
Jun 18, 2023

--

Hello everyone;

overloading

Let’s start by asking the output of the following code block first.

public class OverLoadExample {

public void test(long a, int b)
{
System.out.println("test one");
}
public void test(int a, long b)
{
System.out.println("test two");
}

public static void main(String[] args) {

OverLoadExample overLoadExample = new OverLoadExample();
overLoadExample.test(1,2);
}
}

What do you expect to happen when we run this code block?

A) test one
B) test two
C) Run Time Error
D) Compile Time Error
E) Nothing

Answer is Compile Time Error

When you copy and paste this code your IDE, you see that “Ambiguous method call” error warning.

Yeah Confusing Java :)

Thanx to reading

--

--

Serdar A.
Serdar A.

Written by Serdar A.

Senior Software Developer & Architect at Havelsan Github: https://github.com/serdaralkancode #Java & #Spring & #BigData & #React & #Microservice

No responses yet