Junior — Middle
How is the concept of an object implemented in object-oriented programming language?
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) to work with this data. An object encapsulates state and behavior, allowing modeling of real entities.
For example, in Java, an object is created based on a class:
class Car {
String color;
int speed;
void accelerate() {
speed += 10;
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // creating an object
myCar.color = "red";
myCar.speed = 0;
myCar.accelerate();
System.out.println(myCar.speed); // 10
}
}
Here, myCar is an object of class Car, which stores its state (color, speed) and can perform actions (accelerate).