Junior — Middle
Millised on abstraktsete klasside peamised funktsioonid ja rakendused objektorienteeritud programmeerimises?
sobes.tech AI
Vastus AI-lt
Abstract classes in object-oriented programming are classes that are not intended to be instantiated directly, but serve as base templates for other classes.
Main functions of abstract classes:
- Define a common interface and basic behavior for a group of related classes.
- The ability to declare abstract methods (without implementation), which must be implemented by subclasses.
- Allow partial implementation of functionality inherited by child classes.
Applicability:
- When it is necessary to establish a common contract for different implementations.
- When part of the logic is common, and another part must be implemented in subclasses.
Example in Java:
abstract class Animal {
abstract void makeSound(); // abstract method
void breathe() {
System.out.println("Breathes");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Woof-woof");
}
}
Here, Animal cannot be created directly, but a Dog can be created that implements the makeSound method.