Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

For what is the exact problem due to which Java does not allow such code to compile?

 
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code gives compilation errors  The return type is incompatible with  InterfaceB.print().











I understand that it considers them as duplicate methods but what is the exact reason. Java doesnt allow this kind of code to compile for some reason for sure. What problem would have occurred had Java allowed this kind of code to compiled. Thanks





 
Master Rancher
Posts: 4698
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Which print method is going to be called?
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood that it will not be able to decide which method to call because it calls methods based on signatures and return type does not come in signature.

The question which I should have asked instead is why is the return type not considered the part of the method signature?
 
Saloon Keeper
Posts: 6625
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that "myObj.print()" could actually be calling the method that returns an int - just because a method does have a return value does not mean your code would have to use it.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood. That when we are calling the method we can give only the parameters but cannot specify return type.

Just wondering that for what reason was method calling not kept this way in Java that while calling a method give the parameters and also tell the return type and then call.  Would that have created any problem too?
 
Marshal
Posts: 70598
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way you could have made it possible for the compiler to determine the return type of a method is to insist it be always used explicitly. Then you would have to callThat would have made the language look completely different.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That would have made the language look completely different.



Yes, different.But do you see a problem it could have created and to avoid the problem it was not kept so.?
 
Dave Tolls
Master Rancher
Posts: 4698
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several methods that have return values that I often don't use.
For example, List.remove returns the item removed from the List.
I quite often don't need that, but if I was expected to have to stick the return value somewhere then I would always have to:

Seems a bit pointless.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:There are several methods that have return values that I often don't use.
For example, List.remove returns the item removed from the List.
I quite often don't need that, but if I was expected to have to stick the return value somewhere then I would always have to:

Seems a bit pointless.



And what would you do otherwise ?
 
Saloon Keeper
Posts: 12405
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Java does now: Not make the return type part of the method signature.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:, but if I was expected to have to stick the return value somewhere then I would always have to:

Seems a bit pointless.



I meant the below :

In Java when calling a method we do as below :

Use the combination of method name , parameters.

With this both and  have no way to be differentiated .

Instead what if below was allowed :

For calling the parameter use the combination of method name ,parameters and the return type.
In this way and  does have a way to be differentiated.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:, but if I was expected to have to stick the return value somewhere then I would always have to:

Seems a bit pointless.



I meant the below :

In Java when calling a method we do as below :

Use the combination of method name , parameters.

With this both and  have no way to be differentiated .

Instead what if below was allowed :

For calling the parameter use the combination of method name ,parameters and the return type.
In this way and  does have a way to be differentiated.


But it is been kept so for some reason.I am trying to know that .
 
Campbell Ritchie
Marshal
Posts: 70598
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, maybe, but that would have produced syntax unfamiliar to users who came from the C/C++ world. The intention was for the syntax to look familiar to them. The more strictly syntax is defined, the more errors you get at compile time, and the fewer errors at runtime.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, maybe, but that would have produced syntax unfamiliar to users who came from the C/C++ world. The intention was for the syntax to look familiar to them. The more strictly syntax is defined, the more errors you get at compile time, and the fewer errors at runtime.



Yes.But if this has been so in C/C++ world too then some reason must be there .This way would have created some problem which I am not able to think of .
 
Dave Tolls
Master Rancher
Posts: 4698
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, as I showed in my example, we don't always care about the return value.
Forcing us to handle the return value seems unnecessary.
 
reply
    Bookmark Topic Watch Topic
  • New Topic