Sobes.tech
Junior — Middle

What are the levels of access to methods in object-oriented programming?

sobes.tech AI

Answer from AI

In object-oriented programming (using Java as an example), there are the following access levels for methods:

  • public — the method is accessible from anywhere in the program.
  • protected — the method is accessible within its class, in subclasses, and within the package.
  • default (package-private) — if no modifier is specified, the method is accessible only within the current package.
  • private — the method is accessible only within its class.

Example:

public class Example {
    public void publicMethod() {}
    protected void protectedMethod() {}
    void defaultMethod() {} // package-private
    private void privateMethod() {}
}

Choosing the access level helps control encapsulation and protect the internal implementation details of the class.

What are the levels of access to methods in… - sobes.tech