• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces  RSS feed

 
Radhakrishnan Ranjit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Could somebody help me with the following

Interface A { String test();}
Interface B { int test();}

I need to have an Interface C which implements both A and B. Please help me with a programatical solution.

Thanks
RR
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your compiler will choke if you try to implement both interfaces.

This is an example of overloading. But when overloading you have to change the signature which means changing the parameters passed to the method.



would work.
 
Radhakrishnan Ranjit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sebastian.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian is right. You can easily extend two interfaces, but only if they do not have any conflicting methods. In your case they have.

Let's say it would be possible. You then want to create a class that implements interface C:
And as you well know, that's not allowed in a class, because if you call test() the compiler can't tell which of these two methods to use:
 
Radhakrishnan Ranjit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method is identified by its name and the type of its arguments. So, if two methods have the same name and the same argument types, they are the same, according to the compiler.

The return type is not part of the method signature. So if you have two methods with the same name and the same argument types but different return types, the compiler still regards them as two times the same method.

See Section 8.4.2 of the Java Language Specification for details.
 
Suren Singh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys ,

Any idea why return type also was not considered in identifying which method to invoke ?

Thanks
Suren
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suren Singh wrote:
Any idea why return type also was not considered in identifying which method to invoke ?


"why" questions are generally impossible to answer definitively -- unless of course, we happen to have one of the original designers of Java hanging out at the ranch...

But I can speculate. My speculation is that Java was somewhat modelled after C++, and C++ doesn't suport method overloading by return type.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll speculate even further...

What if you had this:

String methodA() {};
Integer methodA() {};

and then someone called it like this:

Object temp = methodA();

How could the JVM know which version to run?

edit: I now of course expect some of the gurus here to come back with "well, dummy, it would know because of <some uber technical reason i'll never understand>"
 
Suren Singh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess there is no way that JVM can recognize which method to invoke in the above example .

I think its hardly a requirement to overload a method on the basis of return type .

Thanks
Suren
 
Radhakrishnan Ranjit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this kind of approach. Will this be apt in this situation

Interface A

Interface B


Please let me know if I am doing something wrong.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is potentially dangerous to return null. At least returning 0 produces a "real" value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!