Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Know about the access modifier

Author: Info Campus
by Info Campus
Posted: Apr 04, 2017

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 sub classes (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 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.

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;

  2. import pack.*;

  3. class Run

  4. {

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

  6. {

    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. public class Run

  8. {

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

  10. {

  11. Demo obj=new Demo();

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

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

  14. }

  15. }

In above given example we can see that data variable and message() method both are private.So they can’t 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. package pack;

  2. public class Demo

  3. {

  4. public void message()

  5. {

  6. System.out.println("Hello Java");

  7. }

  8. }

java/j2ee classes bangalore

  1. //save by Run.java

  2. packagemypack;

  3. importpack.*;

  4. class Run

  5. {

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

  7. {

  8. Demo.obj=new Demo();

  9. obj.message();

  10. }

  11. }

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. class Run extends Demo

  5. {

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

  7. {

  8. Run.obj=new Run();

  9. obj.message();

  10. }

  11. }

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.

Author : 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 : 9738001024http://www.earticlesonline.com/myarticle.php or for more details on Advance java training visit at: http://www.infocampus.co.in/java-training-bangalore.html

About the Author

A Real Time Software Training Institute 100% Guaranteed JOB Support Get Real Time Training from Industry Experts Book Free Demo Class

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Info Campus

Info Campus

Member since: Feb 08, 2017
Published articles: 45

Related Articles