Junior — Middle
Can you explain what is meant by an 'object' in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming (OOP), an object is an instance of a class that combines data (fields, properties) and methods (functions) that work with this data.
An object represents a specific entity with a state and behavior. The state is defined by the values of its fields, and the behavior by methods that can modify the state or perform actions.
For example, a "Car" class describes general characteristics and actions, and an object is a specific car with a certain color, make, and speed.
Example in Java:
class Car {
String color;
int speed;
void accelerate(int increment) {
speed += increment;
}
}
Car myCar = new Car();
myCar.color = "red";
myCar.speed = 0;
myCar.accelerate(10); // increase speed
Thus, an object is a specific instance of a class with its own state and behavior.