Access modifiers Interview Questions
Q 1=>How many types of modifiers are there in Java.?
If you are talking about only modifiers.Two types of modifiers are available in java. They are,
a) Access Modifiers
b) Non-access Modifiers
Q 2=>What are access modifiers in java.?
These are the modifiers which are used to control the visibility of a class or a field or a method or a constructor. Java supports 4 types of access modifiers.
1:- private : private fields or methods or constructors are visible within the class in which they are defined.
2:- protected : Protected members of a class are visible within the package but they can be inherited to sub classes outside the package.
3:- public : public members are visible everywhere.
4:- default or No-access modifiers : Members of a class which are defined with no access modifiers are visible within the package in which they are defined.
(For more info on access modifiers, click here.)
Q 3=>What are non-access modifiers in java.?
These are the modifiers which are used to achieve other functionalities like,
a) static : This modifier is used to specify whether a member is a class member or an instance member.
b) final : It is used to restrict the further modification of a class or a method or a field. (for more on final, click here).
c) abstract : abstract class or abstract method must be enhanced or modified further. (For more on abstract, click here).
d) synchronized : It is used to achieve thread safeness. Only one thread can execute a method or a block which is declared as synchronized at any given time. (for more on synchronized, click here.)
Q 4=>Can we use a field or a method declared without access modifiers outside the package.?
No, we can’t use a field or a method with no-access (default) specifiers outside the package in which their class is defined.
Q 5=>Can a method or a class be final and abstract at the same time.?
No, it is not possible. A class or a method can not be final and abstract at the same time. final and abstract are totally opposite in nature. final class or final method must not be modified further where as abstract class or abstract method must be modified further.
Q 6=>Can we declare a class as private.?
We can’t declare an outer class as private. But, we can declare an inner class (class as a member of another class) as private.
Q 7=>Can we declare an abstract method as private also.?
No, abstract methods can not be private. They must be public or protected or default so that they can be modified further.
Q 8=> Can we declare a class as protected.?
We can’t declare an outer class as protected. But, we can declare an inner class (class as a member of another class) as protected.
Q 9=>class can not be declared with synchronized keyword. Then, why we call classes like Vector, StringBuffer are synchronized classes.?
Any classes which have only synchronized methods and blocks are treated as synchronized classes. Classes like Vector, StringBuffer have only synchronized methods. That’s why they are called as synchronized classes.
Q 10=>can we declare a variable as public inside main method?
We cannot declare a variable inside a method as public.Variables declared inside main method or method or blocks are called local variables.For Local variable, the only applicable modifier is Final.
Usage of any other modifier(Like public, private,protected, static, transient, volatile) gives you compile time error saying ” Illegal start of Expression”.
After declaring local variable as final, if you are not using it, you need not initialize. You should perform initialization only before using it.
post a comment