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
- What is the purpose of access specifiers in Java?
- Explain the difference between
public
,protected
,default
, andprivate
access specifiers. - When would you use each access specifier?
- Can you override a method with a lower access specifier?
- What is the default access specifier in Java?
- How does the
protected
access specifier differ fromdefault
(package-private)? - Can you have a class with
private
access specifier? - Explain the concept of package-level accessibility in Java.
- How does access specifiers affect inheritance in Java?
- Can you access a
public
method from a subclass located in a different package?
Multiple Choice Questions (MCQs) - Access Specifiers
- Which access specifier allows unrestricted access to classes, methods, and variables from anywhere?
- public
- protected
- default
- private
- Which access specifier allows access within the same package and by subclasses, even if they are in different packages?
- public
- protected
- default
- private
- What is the default access specifier in Java?
- public
- protected
- default
- private
- Which access specifier restricts access to only within the same class?
- public
- protected
- default
- private
- Which access specifier is commonly used for methods or variables that should not be accessed outside the class they are defined in?
- public
- protected
- default
- private
Access Modifiers
- What are access modifiers in Java?
- Explain the purpose of
final
,static
,abstract
, andsynchronized
access modifiers. - When would you use each access modifier?
- Can a method be both
abstract
andstatic
? - What is the difference between
final
andstatic
modifiers? - What is the purpose of the
volatile
keyword in Java? - Explain the significance of the
transient
keyword in Java. - How does the
strictfp
modifier affect floating-point calculations in Java? - What is the purpose of the
native
modifier in Java? - Can you use access modifiers with local variables?
Multiple Choice Questions (MCQs) - Access Modifiers
- Which access modifier indicates that the class, method, or variable cannot be modified or overridden?
- final
- static
- abstract
- synchronized
- Which access modifier ensures that a method or block of code can be accessed by only one thread at a time?
- final
- static
- abstract
- synchronized
- Which access modifier indicates that the class, method, or variable belongs to the class rather than instances of the class?
- final
- static
- abstract
- synchronized
- Which access modifier specifies that a method or class must be implemented by subclasses?
- final
- static
- abstract
- synchronized