Constructor ये एक class का special method है जिस class पर उसे बनाया जाता है उसी class के object को initialize करता है |
जिस class का constructor बना हो, अगर उसी class का जब object जब बनता है तब वो automatically call होता है |
Constructor अपने class के नाम जैसा होता है , लेकिन Constructor का कोई return type नहीं होता |
Syntax for Constructor
class class_name{ ------------ class_name(){ // Constructor //statements; } ------------ }
Example for Constructor
class A{ ------------ A(){ // Constructor //statements; } ------------ }
Constructor के तीन प्रकार
- Default Constructor
- Parameterized Constructor
- Constructor Overloading
Default Constructor
Default Constructor कोई parameter या argument नहीं लेता | ऊपर दिया हुआ program Default Constructor का है |
Program में 'A' नाम का class है और उसका constructor बनाया गया है | जब A class का object बनेगा तब Constructor automatically call होगा |जितनी बार A class का object बनेगा उतनी बार constructor call होता है |
Source Code :Output :class Sample{ int a = 2; int b = 4; Sample(){ //Default Constructor System.out.println("Value of a : " + a); System.out.println("Value of b : " + b); } public static void main(String args[]){ Sample obj = new Sample(); } }
Value of a : 2 Value of b : 4
Parameterized Constructor
Parameterized Constructor में Constructor को parameters pass किये जाते है |
Parameterized Constructor में constructor को अलग-अलग arguments दिए जाते है | इसमें arguments की कोई मर्यादा नहीं होती |
Parameterized Constructor में class के object में parameters की values देनी पड़ती है |
निचे दिए हुए program Addition के लिए दो values initialize करके उनका addition किया गया है |
Source Code :Output :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(); }
Addition of 5 and 8 is 13
3. Constructor Overloading
Constructor Overloading में class में multiple Constructor overloading की जा सकती है , सिर्फ उनकी parameters की संख्या और उनके type अलग-अलग होते है |
Constructor Overloading; Function Overloading के तरह ही होता है |
Source Code :Output :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(); } }
Value of a : 5 Value of b : 0 Value of a : 5 Value of b : 9
Copy Constructor
Java में copy constructor का कोई concept नहीं है | लेकिन constructor में एक object को दूसरें object पर copy किया जा सकता है |
Source Code :Output :class Sample{ int a; int b; Sample(int x, int y){ a = x; b = y; } Sample(Sample s){ //copy constructor System.out.println("copy constructor invoked"); a = s.a; b = s.b; } int add(){ int c = a + b; return c; } public static void main(String[] args){ Sample obj1 = new Sample(2, 10); Sample obj2= new Sample(obj1); System.out.println("Object1 : "+ obj1.add()); System.out.println("Object2 : "+ obj2.add()); } }
copy constructor invoked Object1 : 12 Object2 : 12
Constructor और Method में फर्क क्या है ?
Constructor का नाम class के name जैसा ही होता है | Method का नाम कोई भी हो सकता है |
Constructor; object बनते ही call होता है | Method को call करना पड़ता है |
Constructor कोई value return नहीं करता | Method सभी value return करता है |