Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A class implements two interfaces. Which interface does a method belong to? [duplicate]

Tags:

java

interface

There are two interfaces B and C each having the same method public m1()

class A implements B and C 

If class A has to implement method m1(), the implemented method would be of which interface?

like image 916
Anirudh Avatar asked Nov 18 '13 10:11

Anirudh


2 Answers

I think we can say that A.m1 implements both B.m1 and C.m1. Because both

B b = new A();
b.m1();

and

C c = new A();
c.m1();

will work

like image 67
Evgeniy Dorofeev Avatar answered Nov 14 '22 23:11

Evgeniy Dorofeev


This is a common problem, this is why having clear instructional method names is important. And good OOP design that will make same methods be abstract.

It is also the reason things are separated out in to classes.

Animal.eat()

Fish extends Animal
    Fish.eat()
Dog extends Animal
    Dog.eat()
like image 30
Peter_James Avatar answered Nov 15 '22 01:11

Peter_James