Access Modifiers In Java}

Access Modifiers in java

by

Infocampus

In java there are two types of modifiers :

[1] Access modifiers

[2] 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:

1.default

2.private

3.public

4.protected

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.

Example

Variables and methods can be declared without any modifiers, as in the following examples ‘

//save by Demo

1. package pack;

2. class Demo

3. {

4. void message()

5. {

6. System.out.println(“Hello”);

7. }

8. }

java training center bangalore

//saveby Run.java

1. package mypack;

[youtube]http://www.youtube.com/watch?v=nYVkyLefu2A[/youtube]

2. import pack.*;

3. class Run

4. {

5. public static void main(String args[])

6. {

7 Demo . Obj = new Demo(); //Compile Time Error

7. obj.message(); //Compile Time Error

8. }

9. }

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.

Example

The following class uses private access control ‘

class Demo

{

1. private int data=90;

2. private void message()

3. {

4. System.out.println(“Hello java”);

5. }

6. }

7.

8. public class Run

9. {

10. public static void main(String args[])

11. {

12. Demo obj=new Demo();

13. System.out.println(obj.data); //Compile Time Error

14. obj.message(); //Compile Time Error

15. }

16. }

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.

Example

The following function uses public access control ‘

//save by Demo.java

1.

2. package pack;

3. public class Demo

4. {

5. public void message()

6. {

7. System.out.println(“Hello Java”);

8. }

9. }

java/j2ee classes bangalore

1. //save by Run .java

2.

3. packagemypack;

4. importpack.*;

5.

6. class Run

7. {

8. public static void main (String args[])

9. {

10. Demo.obj=new Demo();

11. obj.message();

12. }

13. }

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

Example

//save by Demo.java

1. package pack;

2. public class Demo

3. {

4. protected void message()

5. {

6. System.out.println(“Hello”);

7. }

8. }

1. //save by Run.java

2. package mypack;

3. import pack.*;

4.

5. class Run extends Demo

6. {

7. public static void main(String args[])

8. {

9. Run.obj=new Run();

10. obj.message();

11. }

12. }

Output: Hello

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

Article Source:

eArticlesOnline.com}

Theme: Overlay by Kaira Extra Text
Cape Town, South Africa