In java we use access modifiers to control the members(method and variables) with in the class.
In java there are two types of modifiers :
 Access modifiers
 Non â€ access modifiers
Access modifiers specifies the accessibility or scope of a data member(non static global variable),method,constructor or class.
Access modifiers are of four types:
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we will learn access modifiers
The four access levels are â'
-Visible to the package, the default. No modifiers are needed
-Visible to the class only (private).
-Visible to the world (public) java/j2ee classes bangalore
-Visible to the package and all subclasses (protected).
Default Access Modifier - No Keyword
Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.For default access modifiers we donâ€™t use any keyword .
A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
Private Access Modifier - Private
Methods, variables, and constructors that are declared private can only be accessed within java course in bangalore the declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.We can access outside the class using setters method
Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.
Public Access Modifier - Public
A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging java training center bangalore to the Java Universe.
However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.
The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.
Protected Access Modifier - Protected
Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. Advance java training in bangalore
Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
Why we use the main method as public?
The reason behind that JVM (Java Virtual Machine)look for the main method. JVM is library and program class is different ,if main() method is not public ,it willnâ€™t be visible to the JVM and JVM willnâ€™t get the main() and through the error as main() method not found.To avoid this we use main method as public.
Why we canâ€™t take the class as private?
We canâ€™t take the class as private because if class is best core java training in bangalore declared as private classloader will be unable to load the class memmbers into the memory(heap memory).
This is the basic about the access modifiers . To learn more interesting topics on java join Infocampus Software training Institute for java course and get 100% job assistance .
Please Rate this Article
5 out of 54 out of 53 out of 52 out of 51 out of 5
Not yet Rated