What is access modifier in JAVA hind ?i-vishmy1.blogspot.com

                         Access Modifiers in JAVA 

The Access Modifiers in JAVA specifies the accessibility or scope of a field(variables), method, or class. we can change the access level of field, constructors, methods, and class by applying the access modifier on it. 

JAVA में एक्सेस संशोधक(Access Modifier) किसी क्षेत्र (चर), विधि(method), या वर्ग(class) की पहुंच(accessibility) या गुंजाइश(scope) को निर्दिष्ट(specifies) करता है। हम इस पर पहुँच संशोधक लागू करके क्षेत्र(field), निर्माणकर्ताओं(constructors), विधियों और वर्ग(class) के पहुँच स्तर(access level) को बदल सकते हैं।

There are 4 types of access modifier in JAVA.


  1. Default.                                                                                           
  2. Private.                                                                                                       
  3. Protected.                                                                                         
  4. Public.


What is access modifier in JAVA hind ?i-vishmy1.blogspot.com
Access Modifiers in JAVA

1 )- Default Access Modifier :- The access level of a default modifier is only within the package. It can't be accessed from outside the package. If you do not specify any access level, it will be the default. If you don't use any modifier, it is treated as default by default. It provides more accessibility than Private but, it is more restrictive than Protected and Public.

एक डिफ़ॉल्ट संशोधक(default modifier ) का पहुंच स्तर(access level) केवल पैकेज(package) के भीतर है। इसे पैकेज(package) के बाहर से एक्सेस(access) नहीं किया जा सकता है। यदि आप कोई पहुंच स्तर(access level) निर्दिष्ट(specify) नहीं करते हैं, तो यह डिफ़ॉल्ट(default) होगा। यदि आप किसी भी संशोधक(modifier) का उपयोग नहीं करते हैं, तो इसे डिफ़ॉल्ट(Default) रूप से डिफ़ॉल्ट(by default) माना जाता है। यह निजी(Private) की तुलना में अधिक पहुंच प्रदान करता है लेकिन, यह संरक्षित(Protected) और सार्वजनिक(Public) की तुलना में अधिक प्रतिबंधात्मक(restrictive) है।

ex :-

In this ex, we will create two packages mypack1 & mypack2. we will aceess the test1 class from outside it's package, since test1 class is not public, so it can't be accessed from outside the class.

इस उदाहरण में, हम दो पैकेज mypack1 और mypack2 बनाएंगे। हम टेस्ट 1 वर्ग(class) के बाहर से इसका पैकेज तैयार करेंगे, क्योंकि टेस्ट 1 क्लास सार्वजनिक(Public) नहीं है, इसलिए इसे क्लास के बाहर से एक्सेस नहीं किया जा सकता है।

how to create package:-

package package_name;

how to import package :-

import package_name;

//save by test1.java

package mypack1; //package created
class test1 //class 
{
     void show()//method
      {
           System.out.println("hello");
      }
}

//save by test2.java

package mypack2;//package created
import mypack1.*;//imported package
class test2//class
{
     public static void main(String args[])
     {
          test1 t1 = new test1();//compile time error
          t1.show();//compile time error
      }
}


2 )- Private Access Modifier :-

The access level of a private modifier is only within the class. It can't be accessed from outside the class. The Private modifier is accessible only within the class.

एक निजी संशोधक(Private Modifier) का उपयोग स्तर(access level) केवल कक्षा(class) के भीतर है। इसे कक्षा(class) के बाहर से एक्सेस नहीं किया जा सकता है। निजी संशोधक(Private modifier) केवल कक्षा(class) के भीतर ही सुलभ(accessible) है।

ex :-

In this ex, we are going to create two classes test & simple, where test class contains private data member & private method, then we will access these member from outside the class

इस उदाहरण में, हम दो कक्षा(class) परीक्षण(test) और सरल(simple) बनाने जा रहे हैं, जहां परीक्षण वर्ग(test class) में निजी डेटा सदस्य(private data member) और निजी विधि(private method) शामिल है, फिर हम कक्षा(class) के बाहर से इन सदस्य(member) तक पहुंच बनाएंगे।

class test 
{
    private int data = 40;
    private void show()
   {
         System.out.println("hello world");
   }
  public class simple
  {
        public static void main(String args[])
        {
             test t = new test();
             system.out.println(t.data);
             t.show();//compile time error
        }
   }
}


3 )- Protected Access Modifier :-

The access level of a Protected Modifier is within the package and outside the package through child class. If you do not make the child class, It can't be accessed from outside the package. The Protected modifier can be applied on the data member, method, & constructor. It can't be applied on the class.

एक संरक्षित संशोधक(Protected Modifier) का पहुंच स्तर(access level) पैकेज(package) के भीतर और बाल वर्ग(child class) के माध्यम से पैकेज(package) के बाहर है। यदि आप चाइल्ड क्लास(child class) नहीं बनाते हैं, तो इसे पैकेज के बाहर से एक्सेस नहीं किया जा सकता है। संरक्षित संशोधक(Protected Modifier)  को डेटा सदस्य(data member), विधि(method) और निर्माणकर्ता(constructor) पर लागू किया जा सकता है। इसे कक्षा(class) पर लागू नहीं किया जा सकता है।

The Protected Modifier is accessible within package and outside the package but through inheritance only.

संरक्षित संशोधक पैकेज(Protected Modifier) के भीतर और पैकेज के बाहर पहुंचता है, लेकिन केवल विरासत(Inheritance) के माध्यम से।

It provides more accessibility than the default modifier.

ex :-

In this ex, we will create two packages mypack1 & mypack2. The test class of mypack1 package is public, so can be accessed from outside the package. But show method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.

इस उदाहरण में, हम दो पैकेज mypack1 और mypack2 बनाएंगे। Mypack1 पैकेज का परीक्षण वर्ग(test class) सार्वजनिक(public) है, इसलिए इसे पैकेज के बाहर से एक्सेस किया जा सकता है। लेकिन इस पैकेज की शो विधि(show method) को संरक्षित(protected) घोषित किया गया है, इसलिए इसे केवल विरासत(inheritance) के माध्यम से कक्षा(class) के बाहर तक पहुँचा जा सकता है।

//save by test.java

package mypack1;
public class test
{
     protected void show()
      {
           System.out.println("hello ");
       }
}


//save by test1.java

package mypack2;
import mypack1,*;
class test1 extends test
{
     public static void main(String args[])
      {
             test1 t1 = new test1();
             t1.show();
       }
}


4 )- Public Access Modifier :-

The access level of public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package. It has the widest scope among all the modifier.

सार्वजनिक संशोधक(public modifier) का पहुंच स्तर(access level) हर जगह है। इसे क्लास के भीतर, क्लास के बाहर, पैकेज के भीतर और पैकेज के बाहर से एक्सेस किया जा सकता है।

ex :-

//save by test.java

package mypack1;
class test
{
   public void show()
   {
        System.out.println("hello");
    }
}


//save by test1.java

package mypack2;
import mypack.*;
class test1
{
     public static void main(String args[])
      {
              test t1 = new test1();
              t1.show();
       }
}

Let's understand the access modifiers in JAVA by a simple table.

What is access modifier in JAVA hind ?i-vishmy1.blogspot.com

Note :- There are many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc. 
                                  
                                                                                    

Please share with your friends and follow this blog for more knowledge.
     
                                                                                      Thank you😃



Post a Comment

0 Comments