Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

Top 20 Java Access Modifier Interview Questions Answers

Access modifiers is one of the essential concept in Java and every programmer should be familiar with, and because of its importance, its also a popular topic in Java interviews. Access modifier can be applied to class, method, fields, and variables and as the name suggests they control the access. Like who can access that particular class, method or variable. For example, public modifier provides universal access which means any public class, method or variable is accessible by everyone and everywhere. In other words, you can access a public class both within the package and outside the package. 


There are total four access modifiers in Java, public, protect, private and package-private. The first three are also keywords used to specify access while last one is the default access modifier, which means if you don't specify an access modifier to any class, method or variable then it will only be accessible with the package.

private is the most restricted and public is the least restricted. A private class, method, or variable is only accessible within the class they are declared. They are not visible outside the class and any attempt to access them outside the class will result in compile-time error.

Similarly, protected modifier provide access which is available to all the classes within the package and only to child classes outside the package. For example, all class inside same package can access protected variables and methods but only child classes can access them outside the package.

Another important point is that private can be applied to class, method, and variable but you cannot make class protected, only methods and fields can be protected. If you are confused what is the difference between a variable and field, then don't worry. They are mostly same, as per convention, class and instance variables are also referred as fields.


 


15 Java Access Modifier Interview Questions

Now that you know the basics about access modifiers in Java like public, private, protected, and package private, its time to see the frequently asked questions about access modifier in Java. I have not provided the answers but most of them are covered in my blog and you can search it. I will also provide link to the answer soon but if you already know the answer, feel free to comment. 

1. how many types of access modifiers is available in Java?

There are four type of access modifiers in Java:

  • public
  • private 
  • protected
  • package-private (default)


2. Difference between public, protected, and private access modifier?

public is the least restricted while private is the most restricted. This means any class, method or field with public modifier is access to all the classes inside and outside the package on which those classes, methods or fields are declared but in case of private they are only accessible within the class they are declared. 


3. What is the default access modifier? What happens if you don't specify access modifier?

Default access modifier in Java is package level access. This is also known as package private. If you don't specify any modifier then its only visible inside the package. For example following class is only visible to other classes within the same package

Calculator{

  // your code 

}

4. Can we make a class private in Java?

Yes, you can make nested classes private in Java but you cannot make a top level class private in Java. They are not allowed. Same goes for interface, you cannot make an interface private in Java as interface are by default and inherently public. 


5. Can we declare a class as protected?

No, we cannot. It will create compile time error. This is true for both top level class and nested class in Java. 


6. What is difference between static and non-static class?

A nested class can be either static or non-static, in that case its known as Inner class. The difference is that you can access static class without creating instance of top level class but you would need an instance of containing class to access a non-static nested class. 


7. What is final modifier, can you use it with classes?

final keyword is not an access modifier but its very important from immutability and performance perspective. Yes, you can make a class final in Java, in that case you cannot extend it further. Final classes in Java cannot be sub classed.


8. Why you should make your field private in Java?

To achieve higher level of encapsulation. If a field or method is private then you can safely change them without affecting others as they are not visible outside the class. 


9. Why getter method is better than public variables in Java?

It provide higher level of abstraction, you can add log statement or return something else like a normalized or calculated value if you want to which cannot be possible if your client is directly accessing the public variable. 

10. Which variables you should mark public in Java?

you can mark constants as public variable like public final int daysInWeek = 7;

11. What is access modifier in Java?

12. How many access modifiers are available in Java?

13. Why its a best practice to by default make a field private in Java?

14) What is difference between public and protected modifier in Java?
Here is a nice table to show the difference between all access modifier in Java

Top 15  Java Access Modifier Interview Questions


15) What is the default modifier in Java?
Its package private access. For example if you don't provide any access modifier then it will visible only on that package. 

16) What is difference between private and package modifier in Java?
private is more restrictive than package private because any package private variable is visible inside the package to other classes but private variables and methods are only visible or accessible on the same class they are declared. 

17) Can we make a field both public and private at same time?
No, because both are mutual exclusive, A variable can not be public and private at the same time. 

18) How does access modifier affects method overriding?
They prevent overriding by restricting access, for example you cannot override private methods in Java. 

19) Which access modifier can be used with top level class?
Only public and package private or default (nothing) can be used with top level class. 

20) What is the advantage and disadvantage of using public modifier in Java?
universal access means you cannot modify them later without impacting other clients which are using it. This is particular true for public classes. For example, removing a constructor of a public class will break existing clients of that class. 

That's all about access modifiers in Java. It's one of the fundamental concept which every Java developer should know and understand. Each modifier has their own use and you should take due diligence in choosing the right access modifier for the job because it will help you in long run while maintaining the application. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.