Constructors क्या हैं? | What is Constructors in Java in Hindi

इस आर्टिकल में हम जावा Constructors क्या हैं | What is Constructors in Java in Hindi के बारे में जानेंगे।

Constructors क्या हैं | What is Constructors in Java in Hindi?

जावा में, constructor method के समान code का एक ब्लॉक है। इसे तब calle किया जाता है जब class का एक instance बनाया जाता है। constructor को कॉल करते समय, object के लिए memory को memory में allocated किया जाता है।

यह एक special type का method है जिसका प्रयोग object को initialize करने के लिए किया जाता है।

हर बार new() keyword का उपयोग करके एक object बनाया जाता है, कम से कम एक constructor को कॉल किया जाता है।

यदि class में कोई constructor उपलब्ध नहीं है तो यह default constructor को कॉल करता है। ऐसे मामले में, जावा कंपाइलर default रूप से एक default constructor प्रदान करता है।

जावा में दो प्रकार के constructors हैं: no-arg constructor और parameterized constructor।

Note: इसे constructor कहा जाता है क्योंकि यह object creation के समय values करता है। class के लिए constructor write जरूरी नहीं है। ऐसा इसलिए है क्योंकि जावा कंपाइलर एक default constructor बनाता है यदि आपकी class में कोई constructor नहीं है तो।

Rules for creating Java constructor in Hindi

constructor defined करने के rules।

  • Constructor का नाम उसके class के नाम के समान होना चाहिए।
  • Constructor के पास कोई return type नहीं होता हैं।
  • Java constructor abstract, static, final और synchronized नहीं हो सकता है
Note: constructor घोषित करते समय हम access modifiers का उपयोग कर सकते हैं। यह object creation को controls करता है। दूसरे शब्दों में, हमारे पास जावा में private, protected, public या default constructor हो सकते हैं।

    Types of Java constructors in Hindi

    जावा में दो प्रकार के constructors हैं:

    1. Default constructor (no-arg constructor)
    2. Parameterized constructor

    What is Constructors in Java in Hindi

      Java Default Constructor in Hindi

      एक constructor को “Default Constructor” कहा जाता है जब उसके पास कोई parameter नहीं होता है।

      A constructor is called “Default Constructor” when it doesn’t have any parameter.

      Syntax of default constructor:

      <class_name>(){}  

      Example of default constructor

      इस उदाहरण में, हम Bike class में no-arg constructor बना रहे हैं। object creation के समय इसे invoked किया जाएगा।

      //Java Program to create and call a default constructor  
      class Bike1{  
      //creating a default constructor  
      Bike1(){System.out.println("Bike is created");}  
      //main method  
      public static void main(String args[]){  
      //calling a default constructor  
      Bike1 b=new Bike1();  
      }  
      }  

      Output

      Bike is created
      Rule: यदि किसी class में कोई constructor नहीं है, तो कंपाइलर automatically रूप से एक default constructor बनाता है।

      Java Default Constructor in Hindi

      Java Parameterized Constructor in Hindi

      एक constructor जिसमें specific number में parameters होते हैं, एक parameterized constructor कहलाता है।

      Why use the parameterized constructor in Hindi? | parameterized constructor का उपयोग क्यों करें?

      parameterized constructor का उपयोग अलग-अलग objects को अलग-अलग values प्रदान करने के लिए किया जाता है। हालाँकि, आप समान values भी प्रदान कर सकते हैं।

      Example of parameterized constructor

      इस example में, हमने Student class का constructor बनाया है जिसमें दो parameters हैं। constructor में हमारे पास कितने भी parameters हो सकते हैं।

      //Java Program to demonstrate the use of the parameterized constructor.  
      class Student4{  
          int id;  
          String name;  
          //creating a parameterized constructor  
          Student4(int i,String n){  
          id = i;  
          name = n;  
          }  
          //method to display the values  
          void display(){System.out.println(id+" "+name);}  
         
          public static void main(String args[]){  
          //creating objects and passing values  
          Student4 s1 = new Student4(111,"Karan");  
          Student4 s2 = new Student4(222,"Aryan");  
          //calling method to display the values of object  
          s1.display();  
          s2.display();  
         }  
      }  

      Output

      111 Karan
      222 Aryan

      Constructor Overloading in Java in Hindi

      जावा में, एक constructor एक method की तरह है, लेकिन return type नहीं  हैं। इसे जावा methods की तरह overloade भी किया जा सकता है।

      जावा में Constructor overloading different parameter lists के साथ एक से अधिक Constructor रखने की technique है। उन्हें इस तरह arranged किया जाता है कि प्रत्येक constructor एक अलग कार्य करता है। वे compiler द्वारा list और उनके types में parameters के number से differentiate होते हैं।

      Example of Constructor Overloading

      
      //Java program to overload constructors  
      class Student5{  
          int id;  
          String name;  
          int age;  
          //creating two arg constructor  
          Student5(int i,String n){  
          id = i;  
          name = n;  
          }  
          //creating three arg constructor  
          Student5(int i,String n,int a){  
          id = i;  
          name = n;  
          age=a;  
          }  
          void display(){System.out.println(id+" "+name+" "+age);}  
         
          public static void main(String args[]){  
          Student5 s1 = new Student5(111,"Karan");  
          Student5 s2 = new Student5(222,"Aryan",25);  
          s1.display();  
          s2.display();  
         }  
      }

      Output:

      111 Karan 0
      222 Aryan 25

      Difference between constructor and method in Java in Hindi

      constructors और methods के बीच कई अंतर हैं। वे नीचे दिए गए हैं।

      S. No.Java ConstructorJava Method
      1.किसी object की state को initialize करने के लिए एक constructor का उपयोग किया जाता है।किसी object के behavior को उजागर करने के लिए एक method का उपयोग किया जाता है।
      2.constructor के पास return टाइप नहीं होना चाहिए।एक method में return type हो सकता हैं।
      3.constructor को implicitly रूप से invoked किया जाता है।method explicitly रूप से invoked की किया जाता है।
      4.यदि आपके पास class में कोई constructor नहीं है तो जावा compiler एक default constructor प्रदान करता है।method किसी भी case में compiler द्वारा provided नहीं की जाती है।
      5.constructor का नाम class के नाम के समान होना चाहिए।method का नाम class के नाम के समान हो भी सकता है और नहीं भी।

      Java Copy Constructor in Hindi

      जावा में कोई copy constructor नहीं होता हैं। हालाँकि, हम C++ में copy constructor की तरह values को एक object से दूसरे object में कॉपी कर सकते हैं।

      जावा में एक object के values को दूसरे में कॉपी करने के कई तरीके हैं। वे हैं:

      • constructor द्वारा
      • एक object के values को दूसरे में assigning करके
      • Object class के clone() method द्वारा
        इस उदाहरण में, हम जावा constructor का उपयोग करके एक object की values को दूसरे में कॉपी करने जा रहे हैं।
      //Java program to initialize the values from one object to another object.  
      class Student6{  
          int id;  
          String name;  
          //constructor to initialize integer and string  
          Student6(int i,String n){  
          id = i;  
          name = n;  
          }  
          //constructor to initialize another object  
          Student6(Student6 s){  
          id = s.id;  
          name =s.name;  
          }  
          void display(){System.out.println(id+" "+name);}  
         
          public static void main(String args[]){  
          Student6 s1 = new Student6(111,"Karan");  
          Student6 s2 = new Student6(s1);  
          s1.display();  
          s2.display();  
         }  
      }  

      Output:

      111 Karan
      111 Karan

      Copying values without constructor in Hindi

      हम किसी object के values को किसी अन्य object को assigning करके एक object के values को दूसरे में कॉपी कर सकते हैं। इस मामले में, constructor बनाने की कोई आवश्यकता नहीं है।

        class Student7{  
            int id;  
            String name;  
            Student7(int i,String n){  
            id = i;  
            name = n;  
            }  
            Student7(){}  
            void display(){System.out.println(id+" "+name);}  
           
            public static void main(String args[]){  
            Student7 s1 = new Student7(111,"Karan");  
            Student7 s2 = new Student7();  
            s2.id=s1.id;  
            s2.name=s1.name;  
            s1.display();  
            s2.display();  
           }  
        }  

        Output

        111 Karan
        111 Karan

        निष्कर्ष

        आशा करता हूँ कि आज के इस आर्टिकल में आपको Constructors क्या हैं | What is Constructors in Java in Hindi से सम्बंधित आपके सारे आंसर मिल गए होंगे। Read More

        आज के इस आर्टिकल में हमने जाना कि Constructors क्या हैं | What is Constructors in Java in Hindi इस आर्टिकल से संबधित आपका कोई प्रश्न हैं तो आप comment करें में आपकी कमेंट का जबाब जरूर दूंगा।

        ऐसे ही IT, Computer Science से रिलेटेड जानकारियाँ पाने के लिए हमारे इस वेबसाइट को सब्सक्राइब कर लीजिये | जिससे आपको हमारी आने वाले नये आर्किल की notification प्राप्त हो सके।

        अगर जानकारी अच्छी लगी हो तो अपने दोस्तों और class mate के साथ ये जानकारी जरूर शेयर करें जिससे उनकी भी हेल्प हो सके।

        FAQ प्रश्न-

        • Constructors क्या हैं | What is Constructors in Java in Hindi?

          जावा में, constructor method के समान code का एक ब्लॉक है। इसे तब calle किया जाता है जब class का एक instance बनाया जाता है। constructor को कॉल करते समय, object के लिए memory को memory में allocated किया जाता है।

        Related Posts

        Leave a Comment