Junior — Middle
Can you explain what the use of the abstract keyword means in programming?
sobes.tech AI
Answer from AI
The keyword abstract in programming, for example in Java, is used to declare abstract classes and methods.
- An abstract class is a class that cannot be instantiated directly and serves as a template for other classes.
- An abstract method is a method without an implementation that must be overridden by subclasses.
This allows creating a common interface and basic functionality, leaving implementation details to subclasses.
Example:
abstract class Animal {
abstract void makeSound(); // abstract method
void breathe() {
System.out.println("Breathing");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
Here, Animal is an abstract class, and makeSound is an abstract method that must be implemented in the Dog class.