简体   繁体   中英

Interface, all methods must be abstract, but one?

I have the situation:

public interface AlarmBroadcasterRC {
    abstract void DataIn(byte[] data);
    abstract void DataOut(byte[] data);
    abstract Boolean Drop(String id);
    abstract Boolean Connected(String id, Boolean state);
}

public class GeneralActivity extends Activity implements View.OnClickListener, AlarmBroadcasterRC {
.....
}

But I get an error from compiler at the class declaration: 在此处输入图片说明

Does it mean that in Android Java an interface MUST implement first method or, if all its methods are abstract, then the class must be abstract also?

Sorry to extend the Q: I was following to one of the answers from here :

如果您有一个实现接口且不是抽象的类,则必须实现其所有抽象方法,而不仅仅是第一个。

In an interface all the methods are by default abstract you don't need to add abstract keyword.

when you implement an interface you have to implement all of it's methods or declare the class as abstract.

What you can do you can provide default implementation of the methods in interface if you don't want to implement in the subclass.

Example:

public interface AlarmBroadcasterRC {
    default void DataIn(byte[] data) { }
    default void DataOut(byte[] data) {}
    default Boolean Drop(String id) {  return false; }
    default Boolean Connected(String id, Boolean state){ return false; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM