Java Access Modifier in Hindi

Java

Java Access Modifier in Hindi – Access modifier sirf kisi class, method, aur constructor ki pravesh yogyata tay karti hai. Access modifier ki wajah se pata chalta hai ki, ek class dusre class ke sath aur ek method dusre method ke sath access ho payegi ya nahi.

Types Of Access Modifiers In Java In Hindi

Access modifier 4 types ke hai.

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

Java main non Access modifier bhi available hai. Java me Static, abstract jaise keyword non access specifier ke tor par kam karte hai.

Public Access Modifier in Java In Hindi

Public Access modifier saare classes, packages, methods, aur constructor ke liye use kiye jate hai. ye modifier package ke aandar aur package ke baher keliye bhi use kiye jate hai. java me public keyword public access modifier ke liye use karte hai.

 Example: Public Access modifier ka Example niche diya hai.

public class World {
  public String code = "java";
  public void world(){
    System.out.println("welcome to java world...");
  }
  public static void main(String[] args) {
    World myWorld = new World();
    System.out.println("code: " + myWorld.code);
    myWorld.world();
  }
}

Public Access modifier ke output niche diye gaye hai.

code: java
welcome to java world...

Protected Access Modifier in Java In Hindi

protected Access modifier sirf package ke liye work hote hai. Package ke bahar ke class ko access nahi kar pate. Ek hi package me jitne bhi classes, method, attributed he unko ek dusre ke sath connect kar sakate hai. Iss access modifier ko protected keyword ke sath java me use kiya jata hai.protected access modifier sab se jyada super class or subclass ko connect karne keliye use hota hai.

Example : Protected Access modifier ka Example niche diya hai.

class World {
  protected String code = "java";
  protected void world(){
     System.out.println("Welcome to java world.");
  }
}
 class Learn extends World {
   protected String learn = "online";
   
    public static void main(String[] args) {
      World myWorld = new World();
      Learn lrn = new Learn();
      System.out.println("code: " + myWorld.code);
      System.out.println("learn: " +lrn.learn);
      myWorld.world();
   }
}

Protected Access modifier ke output niche diye gaye hai.

code: java
learn: online
Welcome to java world.

Private Access Modifier in Java In Hindi

Private access modifier sirf ek hi class ke liye use kiya jata hai. Java me method, attribute, variable ko private karke sirf usi class ke dusre method ke sath access kar sakte hai. Ye access modifier private keyword ke sath access kiya jata hai. 

Example : Private Access modifier ka Example niche diya hai.

class World {
   private String code = "java";
   private void world(){
     System.out.println("welcome to java world.");
  }

  public static void main(String[] args) {
    World myWorld = new World();
    System.out.println("code: " + myWorld.code);
    myWorld.world();
    }  
}

Private Access modifier ke output niche diye gaye hai.

code: java
welcome to java world.

Default Access Modifier in Java In Hindi

Default access modifier package under ki use kiya jata hai. Iss access specifier ko keyword ki jarurat hi nahi hai. Kisi bhi method, class, attribute ko bina keyword ke use kiya gaya to wo attribute default access modifier kehalata hai. Ek hi package ke same classes, methods ko default access modifier use karta hai.  

Example : Default Access modifier ka Example niche diya hai.

class World {
 String code = "java";
 void world(){
 System.out.println("welcome to java world!");
 }
}
  public static void main(String[] args) {
    World myWorld = new World();
    System.out.println("code: " + myWorld.code);
    myWorld.world();
 }

Output:

code: java
welcome to java world.

Java Access Modifier hamesha safety and privacy ke liye use karte hai.

Leave a Reply

Your email address will not be published. Required fields are marked *