Q. why we need access modifier ? or what is purpose of access modifier?
Ans: Access modifier controls the visibility of class and class members. And controlling the accessibility of class and class members relates one of the famous OOPs concept that is Proper Encapsulation.
Q. what are different types of access modifier supported in java.
Ans: There are four different types of access modifier supported in java.
- public.
- private
- default (package-level)
- protected.
Q. Out of four Access modifier which modifier can we use with class ?
Ans: we can use only two access modifier with class
public and default
Q. At this stage some of interviewer may ask some vague question why we can not use private or protected access modifier with class?
Ans: here again you can answer in terms of visibility if class will not be visible to jvm then how it will execute and how other parts of application will use that class.
Q.What can be accessibility order for java access modifier?
Ans:
public : accessible everywhere -- so highly accessible,less restrictive comes at top
protected: Accessible by any class in the same package as its class, and accessible only by subclasses of its class in other packages . so it comes below public access modifier at second position
default: Accessible by any class in the same package as its class, and accessible only by subclasses of its class in other package .it comes at 3rd position below protected.
private: Only accessible in its own class and not anywhere else. so it comes at last position,very less accessibility.
Q. Explain different access modifier in java.
Ans:
public
for detail explanation follow my post here :public access modifier
protected
for detail explanation follow my post here: protected access modifier
for detail explanation follow my post here :default or package level modifier
private access modifier
for detail explanation follow my post here:package access modifier
Accessible inside class where it is declared.
Q: What is difference between public, default and protected access modifier?
or
Q: how you explain different scenarios related with the access modifier in java?
Ans: to know these all answer follow my post here :Access Modifier
If you have any more unanswered questions, then write your questions as comment, i will answers all.
...MORE QUESTIONS TO COME
No comments:
Post a Comment