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, protected, 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.
20 Java Access Modifier Interview Questions with Answers for Beginners 1 to 2 Years Experienced (2023)
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? (answer)
There are four type of access modifiers in Java:
- public
- private
- protected
- package-private (default)
2. Difference between public, protected, and private access modifier? (answer)
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? (answer)
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
public class Calculator{
// your code
}
4. Can we make a class private in Java? (answer)
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? (answer)
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? (answer)
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? (answer)
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? (answer)
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? (answer)
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? (answer)
you can mark constants as public variable like public final int daysInWeek = 7;
11. What is access modifier in Java? (answer)
All the Java keywords which restrict access of an variable, method or class are referred as access modifier in Java. There are 4 access modifier in Java, public, protected, private and package.
- 15 Java Lambda and Stream Interview Questions
- 15 Java Enum Interview Questions for 2 years with Answers
- 10 Java ConcurrentHashMap Interview Questions with Answers
- 13 Java Serialization Interview Questions with answers
- 15 Java IO and NIO Questions from Interviews
- 35 Java String Concept Interview Questions with Answers
- 50 Java Multithreading and Concurrency Interview Questions
- 25 Java Collection Interview Questions with Answers
- 20 Java Generics Interview Questions with Answers
- 25 Java Error and Exception Interview Questions
- 20 Java ArrayList Interview Questions with Answers
- 18 Java Design Pattern Interview Questions with Answers
- 21 Java HashMap Interview Questions with Answers
- 50 Java 8 and Functional Programming Interview Questions
great content
ReplyDelete