Junior — Middle
Kas peaks pakkuma implementatsiooni liidese meetoditele selle baasklassis?
sobes.tech AI
Vastus AI-lt
In Android (and in Java/Kotlin in general), an interface only defines a contract — a set of methods that must be implemented by the class. In classes implementing the interface, these methods must be implemented.
However, if you have a base class that implements the interface, you can provide a default implementation of the interface methods in it. This is useful if multiple subclasses will use the same logic, to avoid code duplication.
For example, in Java, you can do it like this:
public interface MyInterface {
void doSomething();
}
public abstract class BaseClass implements MyInterface {
@Override
public void doSomething() {
System.out.println("Default implementation");
}
}
public class ChildClass extends BaseClass {
// You can override doSomething or use the default implementation
}
Thus, providing an implementation of interface methods in the base class can be useful and logical from an architecture and code reuse perspective.