-2

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: enter image description here

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:

2 Answers2

1

If you have a class that implements an interface and is not abstract, you must implement all of its abstract methods, not just the first.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 1
    @VitaliPetrov In Java 9+ you can provide `default` implementations for those methods in the interface. Otherwise, there is no way; what ought to happen if the method is called, and there is no definition for what that method should do? – kaya3 Feb 02 '20 at 04:03
1

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; }
}
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
  • thank you!!!!!! Sohail, Louis's answer and your's are the same, and to be fair, Louis was just first .. I marked his answer as the Answer, and gave you + 1, Appreciate your help!!! – Vitali Petrov Feb 02 '20 at 04:12