Junior — Middle
Can you explain what an object in Java language is and what role it plays?
sobes.tech AI
Answer from AI
In Java, an object is an instance of a class that contains state (fields) and behavior (methods). Objects allow modeling real entities and interacting with them through methods.
The role of an object in Java is encapsulation of data and logic, which ensures modularity and code reuse. Each object has a unique state, and interactions between objects build the application logic.
Example of creating an object:
public class Car {
String color;
void drive() {
System.out.println("The car is driving");
}
}
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();