In Java, you have got something called access modifier, which specifies accessibility of class, methods and variables. There are four access modifier in Java namely public, private, protected and the default access modifier, also known as package level modifier. Difference between these access modifier comes in their ability to restrict access to a class, method or variables, public is the least restrictive access modifier while package is the most restrictive access modifier, package and protected lies in between. Another key difference between public, protected, package and private modifier comes from the point where you can apply them, for example you cannot use private or protected modifier with a top level class but you can use public modifier there. default modifier is little bit special, when you do not specify any of the public, protected and private modifier, which is also the keywords, then Java automatically apply a default modifier (no it doesn't use default keyword), which means the said class, method or member will only be accessible inside the package it has declared. Any class, which is outside the said package cannot access that element. Good thing about these concept is that, difference between public, protected and private in Java is also one of the frequently asked Java interview question. If you are looking for a Java development position and preparing for one then you can also take help from the wonderful book Java Interview Exposed by Wrox. It is one of the rare complete guide for a Java developer and tells which topic is important from interview point of view.
Read more »
No comments:
Post a Comment