Return to site

☕🎓JAVA CERTIFICATION QUESTION: overloading

· java,ocp

Given:

What will be the output?

* ok the 2024-07-10

* ok the 2024-07-10T07:17:45.523939600

* An exception is thrown

* Compilation fails

#java #certificationquestion #ocp

https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

 

Compilation fails because

'fetch()' clashes with 'fetch()'; both methods have same erasure

'fetch()' is already defined in 'com.vv.FetchService'

The provided code will result in a compilation failure.

The reason is that the FetchService class has two methods named fetch with the same parameter list (i.e., no parameters).

In Java, method overloading requires the methods to have different parameter lists.

Since both fetch methods have identical signatures, this is not allowed.

Here is the problematic part of the code:

These two methods have the same name and parameter list, which leads to a compile-time error due to method signature conflict.

Therefore, the correct answer is:

Compilation fails

Official Oracle tutorial: Overloading Methods

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures.

This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").