• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Interfaces

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sebastian.
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys ,

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

Thanks
Suren
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75867
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is potentially dangerous to return null. At least returning 0 produces a "real" value.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic