Junior — Middle
What properties or characteristics can objects have in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming (OOP), objects have the following main properties and characteristics:
- State — a set of values of the object's fields (attributes) at a given moment.
- Behavior — methods or functions that the object can perform.
- Identity — the uniqueness of the object, allowing it to be distinguished from other objects, even if their state is the same.
For example, in Java, a class describes the structure of an object, and an instance of the class is a specific object with its own state.
Example:
class Car {
String color; // state
int speed;
void accelerate() { // behavior
speed += 10;
}
}
Car myCar = new Car();
myCar.color = "red";
myCar.accelerate();
Here, color and speed are properties of the object, and accelerate() is a method that changes the object's state.