• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface Implementation

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the case of Implementing 2 interfaces on same class each having one method with the same signature.
In this case implementation is allowed as class implementing the interfaces needs to define single method.
Now consider the case when the two method declared in the interfaces defer only in the return types, they have the return types which are in parent child relation ship that means covariant return type.
Can we implement these two interfaces on the same class? I've tried but it gives complier error. I thought that implementing method having wider return type should be possible but it is not so.
Can any one explain me this in detail.
Thanks in advance....
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can not implement these two interfaces,bcos when we implements interfaces on class,they are relate individually with child class, and not with the other interlaces which are being implemented on same class. also if we try to implements both the methods having different return type then it escape javas overloading rule and will give us compile time error.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

covariant return is valid only when overriding a method : the overriding
method is allowed to return a sub-type of the original method. The case you
describe is an invalid overloading (two method differing only by their return
type). That's why you get a compiler error.

Hope it helps

Gilles
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anuj, yes we can implement both the interfaces.
Here is the sample code which got compiled successfully:



Murali...
SCJP5
SCBCD5 Aspirant
reply
    Bookmark Topic Watch Topic
  • New Topic