- Read An Opinion On:
- Faac Gate Automation Australia
Access Modifiers in java
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).
-Visible to the package and all subclasses (protected).
Default Access Modifier – No Keyword
java course in bangalore
Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.For default access modifiers we dont 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.
Variables and methods can be declared without any modifiers, as in the following examples ‘
//save by Demo
1. package pack;
2. class Demo
4. void message()
java training center bangalore
1. package mypack;
2. import pack.*;
3. class Run
5. public static void main(String args)
7 Demo . Obj = new Demo(); //Compile Time Error
7. obj.message(); //Compile Time Error
From above given example we can see that we can access the method message() of class Demo in Run because message() having default access modifiers.
Private Access Modifier – Private
Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Best core java training in bangalore
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.
The following class uses private access control ‘
1. private int data=90;
2. private void message()
4. System.out.println(“Hello java”);
8. public class Run
10. public static void main(String args)
12. Demo obj=new Demo();
13. System.out.println(obj.data); //Compile Time Error
14. obj.message(); //Compile Time Error
In above given example we can see that data variable and message() method both are private .So they cant access outside the class.
Advance java training in bangalore
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 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 following function uses public access control ‘
//save by Demo.java
2. package pack;
3. public class Demo
5. public void message()
7. System.out.println(“Hello Java”);
java/j2ee classes bangalore
1. //save by Run .java
6. class Run
8. public static void main (String args)
10. Demo.obj=new Demo();
Output: Hello Java
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.
Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
core and advanced java institute in marathahalli
//save by Demo.java
1. package pack;
2. public class Demo
4. protected void message()
1. //save by Run.java
2. package mypack;
3. import pack.*;
5. class Run extends Demo
7. public static void main(String args)
9. Run.obj=new Run();
This is the basic about the access modifiers . To learn more interesting topics on java join Infocampus Software training Institute for java courses.
Infocampus provides java course in bangalore with the live project. Attend 4 days free demo classes for java training. Training is given by experienced trainers.Syllabus is according to getting the job easily.Infocampus provides best core java training in bangalore with the 100% placement assistance.Learn Core & Advance Java in the easiest way.Contact at : 9738001024 or for more details on Advance java training visit at: http://www.infocampus.co.in/java-training-bangalore.html