Access Modifiers Questions asked in Interview

Access Specifier Interview Questions Answer

1. How many types of Modifiers in Java?
There are two types of Modifiers in Java:
         a) Access Modifiers
         b) Non- access Modifiers
 
2. What are the access modifiers in java?
Access Modifiers are the modifiers which are used to restrict the visibility of a class, field, method, and constructor.
Java supports four access modifiers:
a) Private: Private members of a class can be accessible only within the class. It cannot be accessed from outside the class.
b) Default: Default members of a class are accessible within the same package due to visible only within the package. They cannot be accessed from outside the package.
c) Protected: Protected members of a class are visible within the package. Therefore, we can only access within the package but can be accessed to the subclasses outside the package through the inheritance only.
d) Public: Public members are visible anywhere. So, we can access anywhere within or outside the package.

3. Can we have a private constructor in Java?
Yes, we can have a private constructor in Java. 
1. The private constructor is used when you do not want to create the object of that class.
2. We cannot create a subclass of that class.
3. It is also used in singleton design pattern, Factory method design pattern.

4. Which access specifier can be used with a class?
Public and Default access specifiers can be used with a class.

5. Can we instantiate the object of derived class if the parent constructor is protected?
No, we cannot instantiate the object of derived class if the parent constructor is protected.

6. What are non-access modifiers in Java?
There are four non-access modifiers in Java. They are as follows:
a. Static: This modifier is used to check that a member is a class member or instance member. If you declare a class as static, this class will be executed first.
b. Final: Final is a keyword which is used to restrict the users. In other words, It is used to restrict further modification of a class, field or method. If a class is declared as 'final', the class cannot be subclassed. 
c. Abstract: Abstract is a keyword that is used with a class or a method. An abstract class or abstract method is used for further modification. If a class is declared as 'abstract', the class cannot be instantiated.
d. Synchronization: It is used to achieve the thread safeness. Only one thread can enter in a synchronized method or block at a given time.

7. Can we declare a top-level class as private?
No, we cannot declare a top-level or outer class as private. It can have either "public" or no modifier. If you declare a top-level class as a private, the compiler will complain that the "modifier private is not allowed here" but an inner class can be private. Inner class means class as a member of another class. Same is the case with protected.

8. Can we declare an abstract method as private?
No, an abstract method cannot be private. They must be declared as public, protected or default so that they can be further modified.

9. Can we declare a top-level class as protected?
No, we cannot declare a class as protected. An inner class can be protected but not outer class.

10. Can a method or a class be final and abstract at the same time?
No, it is not possible. A class or a method cannot be final or abstract at the same time because the final method or final class cannot be further modified whereas an abstract class or an abstract method must be modified further.

11. Why are access modifiers used?
The access modifiers are used to restrict the access of a class and its members. Access modifiers are used to reduce the visibility of the members of a class.

12. Which is the default access modifier?
'Internal' is the default access modifier if no access modifier is mentioned with a class or its members. Internal is a keyword which is used for declaration.

13. What is the default access modifier for Interface?
The public is the default access modifier for the interface. No other access modifier is allowed for them

14. Can we define struct members as protected?
No, we cannot define struct members as protected because struct does not support inheritance.



15. What is the default access specifier for a class, an interface, and struct declared directly with a namespace?
Internal

16. What is the access modifier for enumeration?
Enumeration members are always public. No other access modifiers are allowed.

17. What is the role of private constructor in Java?
If you declare any constructor of a class as a private then you cannot create the object of a class from outside the class. In other words, we cannot create the subclass of that class.

18. Which is the least restrictive access modifier in Java?
Public

19. Which is the most restrictive access modifier in Java?
Private

20. Which access modifier is also known as Universal access modifier?
Public

21. Explain visibility control in Java.
Visibility control in Java is implemented by the access specifier.

Final words
We hope that this article has covered almost all the important Access modifiers interview questions and will help you in an interview. You always remember that when your concepts are clear then you may answer any questions.