Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Interfaces

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiple inheritance in not allowed in java. I now the reason, its because it might be a case that two super classes of a subclass may have methods with same name and thus ambiguity arises when these methods are called.
This can happen with interfaces also, but still it is allowed to a class to implement multiple interfaces!!!
My actual problem is in this code-

ERROR because eat() is defined two times in the class Horse.
now please tell me how to resolve error in this code?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:Multiple inheritance in not allowed in java. I now the reason, its because it might be a case that two super classes of a subclass may have methods with same name and thus ambiguity arises when these methods are called.
This can happen with interfaces also, but still it is allowed to a class to implement multiple interfaces!!!
My actual problem is in this code-

ERROR because eat() is defined two times in the class Horse.
now please tell me how to resolve error in this code?



If you define the same method in two interfaces, make sure that return type also is same, or else it will not get compiled
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okey...thanks Sachin. But if...in case we didn't write the interfaces. They are written by another programmer. We just have to implement them. What can be done then?
 
sachin potu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i am not wrong , At that time you cannot do any thing, except having two different implementations.

like, HorseImplAnimal and HorseImplBird
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah...I too think so

thanks
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sachin potu wrote:if i am not wrong , At that time you cannot do any thing, except having two different implementations.

like, HorseImplAnimal and HorseImplBird



or like Pegasus...
have you tried to reference the interface by (Bird) SomeHorse.eat() or (Animal)SomeHorse.eat()?
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christopher Arthur wrote:

or like Pegasus...
have you tried to reference the interface by (Bird) SomeHorse.eat() or (Animal)SomeHorse.eat()?



not getting you. Are you telling to upcast the Horse object to the objects of Bird and Animal?
Even if we try that, error remain in definition of class Horse because here eat() is defined two times.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Part of the problem here is a poorly designed interface.

If you think about it, it would make sense for the general meaning of the method eat() to be the same for both an Animal and a Bird. In which case, it would be possible to have a single methodthat is an acceptable implementation of the eat() method in both interfaces. The only reason that you can't is that they have different return types. But why? Why does eat() return an int in the Bird class? That doesn't make any sense.
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Linked-list class implementing both Queue and List interface.
Did you ever encountered any method with same name here in both interface but with different return type?
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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