Junior — Middle
Can you give an example of defining an interface in programming?
sobes.tech AI
Answer from AI
In programming, an interface defines a set of methods that a class must implement without specifying the actual implementation. In Android (Java/Kotlin), it is often used to create contracts between components.
Example of an interface in Java:
public interface ClickListener {
void onClick(View view);
}
A class implementing this interface must define the onClick method:
public class ButtonHandler implements ClickListener {
@Override
public void onClick(View view) {
// Reaction to button press
}
}