जावा constructor का परिचय हिंदी में (Introduction of java Constructor)


जावा constructor का परिचय
(Introduction of java Constructor)

constructor जावा class का ही एक method होता है जो उसी class के नाम का बनता है जिस class के अंदर ये method declare किया जाता है |

constructor को आसानी से समझने के लिए इन्हें point में लिख कर समझने का प्रयास करते हैं |

1-       constructor जावा class का एक method होता है |
2-       constructor का नाम भी class के नाम पर ही रखा जाता है आप कोई और नाम नहीं रख सकते हैं |
3-       constructor जिस class के अंदर declare किये जाते हैं उसी class के object के द्वारा ये constructor            automatically call किये जाते हैं |
4-       प्रोग्राम के अंदर जितनी बार object बनेगा उतनी बार ये constructor कॉल होगा |
5-       constructor virtual नहीं होते हैं
6-       constructor को किसी और class के द्वारा inherit नहीं किया जा सकता है |
7-       जावा के हर प्रोग्राम में एक constructor होता है
8-       जब हम प्रोग्राम में constructor declare नहीं करते हैं तो जावा compilier अपने आप प्रोग्राम में एक  
          default constructor generate कर देता है |

जावा constructor के प्रकार :-
(Type of java constructor)

जावा constructor निम्न प्रकार के होते हैं –

1-      Default Constructor
2-      Parameterized Constructor
3-      Copy Constructor
4-      Constructor Overloading

Default Constructor

Default Constructor वो constructor होते हैं जो जावा प्रोग्राम में declare नहीं किये जाते है बल्कि जावा compiler अपने आप generate करता है और default constructor में न कोई arguments होत हैं और न कोई parameters नहीं होते हैं

जितनी बार class का object बनेगा उतनी बार ये constructor कॉल होगा |

class DefaultConstructor
{
                int a = 2;
                int b = 4;
               
                DefaultConstructor(){  //Default Constructor

                System.out.println("Value of a : " + a);
                System.out.println("Value of b : " + b);
                }
                public static void main(String args[])
               {
                DefaultConstructor obj = new DefaultConstructor();
                }
}


Parameterized Constructor

Parameterized Constructor में constructor को parameters और arguments दिए जाते हैं इसमें arguments की कोई लिमिट नहीं होते है |

इसमें class के object में parameters की value देनी होती है  यही parameters की value object के initialization में मदद करती है  इन constructor का प्रयोग करके अलग अलग object members को अलग अलग value दे सकते हैं

class A{

    int a, b, c;

    A(int x, int y)   //Parameterized Constructor
                {   
    a = x;
    b = y;
    c = a + b;
                }
                                void display()
                                {
                                System.out.println("Addition of " + a + " and " + b + " is " + c);
                                }
                }
                public static void main(String args[])
                {
                A a(5, 6);
                a.display();
                }             


Copy Constructor

Copy Constructor  का प्रयोग class के एक object की value को उसी class के दुसरे object में पास करने के लिए किया जाता है |

Class Person
{
String Name;
Int age;
Person(String n, int a)
{
Name =  n;
age = a;
}
Person (Person p) // copy constructor for p2 object
{
Name = p.Name;
age = p.age;
}
Public void display()
{
System.out.println(“Name is ”+Name+”Age is “+age);
}
}
Class CopyConstructor
{
Public static void main(String[]args)
{
Person p1 = new Person(“kamal”,age);
Person p2 = new Person(p1);
P1.display();
P2.display();
}
}


Constructor Overloading

Constructor overloading में एक ही class में एक से ज्यादा constructor बनाने के लिए किया जाता है |
इस constructor में जब एक से ज्यादा constructor बनाये जाते हैं तो उनके parameters की संख्या और उनके टाइप अलग अलग होते हैं और ये function overloading कि तरह काम करता हैं |

class Sample{
               
                int a; 
    int b; 
   
                Sample(int x)
                { 
    a = x; 
    } 
    Sample(int x, int y)
                { 
    a = x; 
    b = y;
    } 
    void show()
                {
                                System.out.println("Value of a : " + a);
                                System.out.println("Value of b : " + b);
                } 
  
    public static void main(String args[])
                { 
    Sample s1 = new Sample(5); 
    Sample s2 = new Sample(5, 9); 
    s1.show(); 
    s2.show(); 
   } 
}             



अगर आपको हमारी यह पोस्ट अच्छी लगी हो तो प्लीज अपने दोस्तों के साथ भी इसे शेयर करे 
धनयबाद || 


जावा constructor का परिचय हिंदी में (Introduction of java Constructor) जावा constructor का परिचय हिंदी में  (Introduction of java Constructor) Reviewed by Unknown on October 02, 2018 Rating: 5

1 comment:

Theme images by fpm. Powered by Blogger.