Top 20 Java Access Modifier Interview Questions Answers for 1 to 2 Years Experienced
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
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
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.
12. How many access modifiers are available in Java? (answer)
There are 4 access modifier in Java, public, private, protected and
package. By default all the variables, methods or class have package level
access which means if you don’t specify any access modifier then they will
be accessible only in the package they are declared.
13. Why its a best practice to by default make a field or method
private in Java? (answer)
When a field is private this means you know that no one else outside the
class using that variable, method or class which means it can easily be
changed without much impact to outside world.
14. What is difference between public and protected modifier in
Java? (answer)
Here is a nice table to show the difference between all access modifier in
Java

15. What is the default access modifier for class, variables, and methods in Java? (answer)
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? (answer)
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? (answer)
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? (answer)
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? (answer)
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? (answer)
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 different types of access modifiers in Java. In this article, we have not only see access modifiers like public, private, protected and package level access but also final modifier which is an important keyword in Java for creating Immutable classes. Both access modifier and final moodier are 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.
Other Core Java Interview Questions you may like to prepare
Thanks a lot for reading this article so far. If you like this Java Access modifier Interview Questions about access modifiers then please share with your
friends and colleagues. If you have any questions which is not answered
here, feel free to ask in comments, I will try my best to answer your
doubts.
these Java Interview online courses, which contains more than 200+ real-world questions from Java interviews and their explanation.
Posting Komentar untuk "Top 20 Java Access Modifier Interview Questions Answers for 1 to 2 Years Experienced"