Junior — Middle
What is the main function of an object in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming (OOP), the main function of an object is to combine data and behavior related to a specific entity into a single structure.
An object is an instance of a class that contains:
- State — the values of fields (attributes) describing the characteristics of the object.
- Behavior — methods that define what the object can do.
Thus, an object models a real or abstract entity, allowing interaction through methods while encapsulating internal implementation details.
Example in Dart (Flutter):
class Car {
String model;
int year;
Car(this.model, this.year);
void drive() {
print('$model is driving');
}
}
void main() {
var car = Car('Toyota', 2020);
car.drive(); // Outputs: Toyota is driving
}
Here, the car object combines data (model and year) and behavior (the drive method).