CoreJava - Access Specifiers
Core Java – Access Specifiers or Modifiers 1. What is a public access specifier in Java? A: The public access specifier in Java allows a class, method, or variable to be accessible from any other class or package. For example, a public method in a class can be called from any other class. Example: public class Car { public void startEngine() { System.out.println("Starting engine..."); } } In this example, the startEngine method is public, which means it can be called from any other class. 2. What is a private access specifier in Java? A: The private access specifier in Java restricts access to a class, method, or variable to only within the same class. For example, a private variable in a class can only be accessed from within that class. Example: public class Car { private int mileage; ...