What if we have 2 interfaces with same methods in them and a class implements both of them ,then what will happen ? | Core Java Forum
M
mohit thakur Posted on 27/12/2021

What if we have 2 interfaces with same methods in them and a class implements both of them ,then what will happen ?


Y
Yogesh Chawla Replied on 28/12/2021

We can't have same method with same parameters in same class so if two interfaces are having same method declaration with same parameters then we can't do even method overloading in that class(that will give us compilation error anyhow) so we will have only one method implementation.

And we have to follow the rulebook given by interfaces so anyhow that method is already implemented by a class.

Note: We should never have two interfaces with same methods, that is actually not a good programming practice. That is a lapse at programmer's end that he/she hasn't checked the other interfaces present with same method. 

Check this:

interface ParentInterface1{
	void sampleMethod();
}
interface ParentInterface2{
	void sampleMethod();
}

public class ClassImplTwoInterfaces implements ParentInterface1,ParentInterface2{

	@Override
	public void sampleMethod() {
		System.out.println("We can't have same method with same parameter in same class "
				+ " so class will only have one implementation only");
	}

}