Junior
Kuidas saab tõestada, et abstraktse klassi meetodid on kohustuslikud üle kirjutada?
sobes.tech AI
Vastus AI-lt
In Android (Java/Kotlin), an abstract class can contain abstract methods, which are methods without implementation that must be overridden in subclasses. If a subclass does not implement these methods, the code will not compile.
Example in Java:
abstract class Animal {
// Abstract method, mandatory to override
abstract void makeSound();
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Woof-woof");
}
}
// If you try to create a class without implementing makeSound(), there will be a compilation error
Thus, the presence of an abstract method forces all subclasses to implement it, ensuring polymorphism and adherence to SOLID principles.