hard to resolve method signature conflict in two interfaces

sometimes, it need implement two interfaces, like:

[code]
public interface IAface {
public static final String A = "AFACE";
public int doSmile();
}

public interface IBface {
public static final String B = "BFACE";
public String doSmile();
}
[/code]

usually, it can be done like:

[code]
public class Face implements IAface, IBface {
public int doSmile() {
...
}

public String doSmile() {
...
}
}
[/code]

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:
[code]
public abstract class MyAface implements IAface {
public int doSmile() {
return -1;
}

public abstract int myDoSmile();

}
[/code]

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

public String doSmile() {
...
}
}
[/code]

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

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?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值