Junior — Middle
What is the concept of calling a method inside a class in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, calling a method inside a class means invoking a function defined within that class or its ancestors to perform a specific action on the object. A method is the behavior of an object, and calling it allows the object to execute operations on its data.
For example, in Java, calling a method inside a class looks like:
public class Car {
private int speed;
public void accelerate(int increment) {
speed += increment;
}
public void drive() {
accelerate(10); // calling another method of the same class
System.out.println("Current speed: " + speed);
}
}
Here, the drive method calls another method accelerate of the same class to change the object's state. Such calls help organize code more structurally and reuse logic within the class.