Junior
How can it be demonstrated that the methods of an abstract class are mandatory to override?
sobes.tech AI
Answer from AI
In Android (Java/Kotlin), an abstract class can contain abstract methods — 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, must be overridden
abstract void makeSound();
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Woof");
}
}
// If you try to create a class without implementing makeSound(), it will result in a compilation error
Thus, having an abstract method requires all subclasses to implement this method, ensuring polymorphism and adherence to SOLID principles.