Encapsulation और access modifiers

Java में Object-Oriented Programming परिचय

Sani Yusuf

Lead Software Engineering Content Developer

Encapsulation समझना

  • Encapsulation को मोबाइल डिवाइस से समझा जा सकता है
  • आप सिर्फ एक्सपोज़्ड फीचर्स जैसे स्क्रीन, स्पीकर आदि तक पहुँचते हैं
  • अंदर की कार्यप्रणाली यूज़र्स से पूरी तरह छिपी रहती है

मोबाइल फ़ोन

Java में Object-Oriented Programming परिचय

Public properties

  • public मार्क की गई properties को object instances से एक्सेस किया जा सकता है

  class Car {
    // Public property
    public String color;


// Public constructor public Car(String color){ this.color = color; } }

    public class Main {

      // main method
      public static void main(String[] args) {
          Car myCar = new Car("brown");
          // ऑब्जेक्ट इंस्टेंस से 
          // public मेंबर्स एक्सेस कर सकते हैं
          System.out.println(
            myCar.color); // Brown
        }
      }

Java में Object-Oriented Programming परिचय

Private properties

  • private मार्क की गई properties को object instances से एक्सेस नहीं किया जा सकता
  • private properties का उपयोग सिर्फ उसी class के भीतर हो सकता है

  // Car class
  class Car {
     public String color;

// Private properties private String model;
public Car(String color){ this.color = color; } }

    // Main class
    public class Main {
      // main method
      public static void main(String[] args) {
          Car myCar = new Car("brown");
          // private properties कॉल करने पर एरर होगा        
          System.out.println(
            myCar.model); // Java compilation error
        }
      }


Java में Object-Oriented Programming परिचय

Public methods


  // Car class
  class Car {
      public String color;
      private String model;

      public Car(String color){
          this.color = color;
      }

      // Public method
      public String getModel(){
          return this.model;
      }    
  }    

Java में Object-Oriented Programming परिचय

Private methods


  class Car {


// Private method, "calculateSpeed" सिर्फ "Car" class के भीतर उपयोग हो सकता है private calculateSpeed(){ // Trademarked formula code } public int getSpeed(){ // "calculateSpeed" को "Car" class में कहीं भी उपयोग कर सकते हैं return this.calculateSpeed(); } }
Java में Object-Oriented Programming परिचय

Static methods

  • static मार्क की गई properties को बिना object instance के एक्सेस किया जा सकता है
  • आमतौर पर shared code/libraries रखने के लिए उपयोग होता है

   // Formula class 
   static class Formula {

     // Square निकालने की method
     static int getSquare(int number) {
        return number * number;
      }
   }





  // Main class 
  public class Main {

    public static void 
      main(String[] args) {
          // "getSquare" को 
          // बिना object instance के उपयोग कर सकते हैं
        System.out.println(
          Formula.getSquare(5)); // 25
      }
  }

Java में Object-Oriented Programming परिचय

Recap

  • Access modifiers: public, private, static
  • Access modifiers ऐप्लिकेशन्स को सुरक्षित रखने में मदद करते हैं
  • static से बिना instances बनाए classes का उपयोग संभव होता है
Java में Object-Oriented Programming परिचय

अभ्यास करते हैं!

Java में Object-Oriented Programming परिचय

Preparing Video For Download...