This is a very interesting question. If you still don't get it let me give an example to explain it further -
We have two interfaces - InterfaceA and InterfaceB. Then we have a concrete class which implements these interfaces.
Code of InterfaceA
package testCodes;
public interface InterfaceA {
public void color();
}
Code for InterfaceB
package testCodes;
public interface InterfaceB {
public void color();
}
Code for class ConcreteClass
package testCodes;
public class ConcreteClass implements InterfaceA, InterfaceB {
public void color() {
System.out.println("Red");
}
}
Note that though we have same method in both interface we need to implement it only once in our concrete subclass. So it does not matter if we have two interfaces with same method. It will be implemented only once.
We have two interfaces - InterfaceA and InterfaceB. Then we have a concrete class which implements these interfaces.
Code of InterfaceA
package testCodes;
public interface InterfaceA {
public void color();
}
Code for InterfaceB
package testCodes;
public interface InterfaceB {
public void color();
}
Code for class ConcreteClass
package testCodes;
public class ConcreteClass implements InterfaceA, InterfaceB {
public void color() {
System.out.println("Red");
}
}
Note that though we have same method in both interface we need to implement it only once in our concrete subclass. So it does not matter if we have two interfaces with same method. It will be implemented only once.
Problem arises when.....
Problem will arise when there are two interfaces having method with same name and parameter but different return type. When a class will implement these two interface it will have to implement these two methods with same name and parameters but different return type. But this is not allowed. JVM will not allow this code to be compiled. Functions can have same name(function overloading) but they must have different signatures(different parameters.)
No comments:
Post a Comment