Hard to resolve method signature conflict in two interfaces

来源:互联网 发布:黑帽seo基础知识 编辑:程序博客网 时间:2024/05/17 01:16
sometimes, it need implement two interfaces, like:

public interface IAface {  public static final String A = "AFACE";  public int doSmile();}public interface IBface {  public static final String B = "BFACE";  public String doSmile();}


usually, it can be done like:

public class Face implements IAface, IBface {  public int doSmile() {    ...  }  public String doSmile() {    ...  }}


then Java compile will complain on can't different those two methods which you must implement in your concrete class, so here have two choices:

1. if those two interfaces are own by you, then you can change the signature of those methods

2. otherwise, you can write a abstract class first like:
public abstract class MyAface implements IAface {  public int doSmile() {    return -1;  }  public abstract int myDoSmile();}


then let your concrete class extends your abstract class and implements IBface again like:
public class Face2 extends MyAface implements IBface {  public int myDoSmile() {    ...  }    public String doSmile() {    ...  }}


this fix the name conflict, but lost interface value because when do like:
  IAface aface = new Face2();  aface.doSmile(); //ok  aface.myDoSmile(); //can't do like this


it may even worse when you haven't the right to change source code of IAface and IBface, so seems pay attention to naming in programming is so important, but sometimes it can't guarantee all names won't conflict especially when you want to extend/implement others' code

what's your insight?
原创粉丝点击