Access Specifiers and Modoifiers - Notes by Shariq SP

Access Specifiers and Access Modifiers in Java

Access specifiers and access modifiers play crucial roles in controlling the visibility and behavior of classes, methods, and variables in Java. Let's explore their definitions, usage, and differences.

Access Specifiers

Access specifiers determine the accessibility of classes, methods, and variables in Java. There are four access specifiers:

Access Specifier Usage Accessibility
public Accessible from anywhere Global accessibility
protected Accessible within the same package and by subclasses (even if they are in different packages) Package and subclass accessibility
default No keyword required; default access level if no access specifier is specified Package-level accessibility
private Accessible only within the same class Class-level accessibility

Access Modifiers

Access modifiers affect the behavior of classes, methods, and variables. Common access modifiers in Java include:

Access Modifier Description
final Makes the class, method, or variable unmodifiable.
static Makes the method or variable shared among all instances of the class, or the class itself, respectively.
abstract Specifies that a method or class must be implemented by subclasses.
synchronized Ensures that only one thread can access a method or block of code at a time.

Differences between Access Specifiers and Access Modifiers

Aspect Access Specifiers Access Modifiers
Definition Determine the accessibility of classes, methods, and variables. Modify the behavior of classes, methods, and variables.
Examples public, protected, default, private final, static, abstract, synchronized, etc.
Usage Specify who can access the class, method, or variable. Control various aspects such as inheritance, method overriding, variable behavior, and concurrency.
Scope Primarily control visibility levels. Affect both visibility and behavior.

Examples of Access Specifiers in Java

Public Access Specifier

The public access specifier allows unrestricted access to classes, methods, and variables from anywhere.


            public class PublicExample {
                public int publicVar = 10; // public variable
                public void publicMethod() { // public method
                    System.out.println("This method can be accessed from anywhere.");
                }
            }
            

Protected Access Specifier

The protected access specifier allows access within the same package and by subclasses, even if they are in different packages.


            public class Parent {
                protected int protectedVar = 20; // protected variable
                protected void protectedMethod() { // protected method
                    System.out.println("This method can be accessed within the same package and by subclasses.");
                }
            }
            

Default (Package-Private) Access Specifier

The default access specifier (no modifier) allows access only within the same package.


            class DefaultExample {
                int defaultVar = 30; // default variable
                void defaultMethod() { // default method
                    System.out.println("This method can be accessed within the same package.");
                }
            }
            

Private Access Specifier

The private access specifier restricts access to only within the same class.


            public class PrivateExample {
                private int privateVar = 40; // private variable
                private void privateMethod() { // private method
                    System.out.println("This method can only be accessed within the same class.");
                }
            }
            

Examples of Access Modifiers in Java

Final Modifier

The final modifier indicates that the class, method, or variable cannot be modified or overridden.


            public class FinalExample {
                final int MAX_VALUE = 100; // final variable
                final void display() {     // final method
                    System.out.println("This method cannot be overridden.");
                }
            }
            

Static Modifier

The static modifier indicates that the variable or method belongs to the class rather than instances of the class.


            public class StaticExample {
                static int count = 0; // static variable
                static void incrementCount() { // static method
                    count++;
                }
            }
            

Abstract Modifier

The abstract modifier indicates that the class or method does not have a complete implementation and must be extended or overridden by subclasses.


            abstract class Shape {
                abstract void draw(); // abstract method
            }
            class Rectangle extends Shape {
                void draw() {
                    System.out.println("Drawing Rectangle");
                }
            }
            

Synchronized Modifier

The synchronized modifier ensures that only one thread can access the method or block of code at a time, preventing concurrent access issues.


            public class SynchronizedExample {
                private int count = 0;
                public synchronized void incrementCount() { // synchronized method
                    count++;
                }
                public void someMethod() {
                    synchronized(this) { // synchronized block
                        // Critical section of code
                    }
                }
            }
            

Interview Questions on Access Specifiers and Modifiers

Access Specifiers

  1. What is the purpose of access specifiers in Java?
  2. Explain the difference between public, protected, default, and private access specifiers.
  3. When would you use each access specifier?
  4. Can you override a method with a lower access specifier?
  5. What is the default access specifier in Java?
  6. How does the protected access specifier differ from default (package-private)?
  7. Can you have a class with private access specifier?
  8. Explain the concept of package-level accessibility in Java.
  9. How does access specifiers affect inheritance in Java?
  10. Can you access a public method from a subclass located in a different package?

Multiple Choice Questions (MCQs) - Access Specifiers

  1. Which access specifier allows unrestricted access to classes, methods, and variables from anywhere?
    1. public
    2. protected
    3. default
    4. private
  2. Which access specifier allows access within the same package and by subclasses, even if they are in different packages?
    1. public
    2. protected
    3. default
    4. private
  3. What is the default access specifier in Java?
    1. public
    2. protected
    3. default
    4. private
  4. Which access specifier restricts access to only within the same class?
    1. public
    2. protected
    3. default
    4. private
  5. Which access specifier is commonly used for methods or variables that should not be accessed outside the class they are defined in?
    1. public
    2. protected
    3. default
    4. private

Access Modifiers

  1. What are access modifiers in Java?
  2. Explain the purpose of final, static, abstract, and synchronized access modifiers.
  3. When would you use each access modifier?
  4. Can a method be both abstract and static?
  5. What is the difference between final and static modifiers?
  6. What is the purpose of the volatile keyword in Java?
  7. Explain the significance of the transient keyword in Java.
  8. How does the strictfp modifier affect floating-point calculations in Java?
  9. What is the purpose of the native modifier in Java?
  10. Can you use access modifiers with local variables?

Multiple Choice Questions (MCQs) - Access Modifiers

  1. Which access modifier indicates that the class, method, or variable cannot be modified or overridden?
    1. final
    2. static
    3. abstract
    4. synchronized
  2. Which access modifier ensures that a method or block of code can be accessed by only one thread at a time?
    1. final
    2. static
    3. abstract
    4. synchronized
  3. Which access modifier indicates that the class, method, or variable belongs to the class rather than instances of the class?
    1. final
    2. static
    3. abstract
    4. synchronized
  4. Which access modifier specifies that a method or class must be implemented by subclasses?
    1. final
    2. static
    3. abstract
    4. synchronized