Junior — Middle
Can you explain what the concept of an abstract class means in object-oriented programming?
sobes.tech AI
Answer from AI
An abstract class in object-oriented programming is a class that is not intended to be instantiated directly. It serves as a template for other classes and can contain both implemented methods and abstract methods (without implementation) that must be implemented in subclasses.
For example, in Dart (the language used in Flutter), an abstract class is declared using the abstract keyword:
abstract class Animal {
void makeSound(); // abstract method
void breathe() {
print('Breathing'); // implemented method
}
}
class Dog extends Animal {
@override
void makeSound() {
print('Bark');
}
}
void main() {
// Animal animal = Animal(); // Error: cannot instantiate an abstract class
Dog dog = Dog();
dog.makeSound(); // Bark
dog.breathe(); // Breathing
}
Thus, an abstract class defines a common interface and basic functionality, while concrete classes implement the details.