Constructors in Java in Hindi- Constructors क्या है ?

Last Updated on May 29, 2022 by RAJENDRAPRASAD

Constructors in Java in Hindi – Hello दोस्तों rajhindime.in में आपका स्वागत है |

दोस्तों, पिछले पोस्ट  Control Statements in Java in Hindi में आपने Control Statement तथा उसके कुछ types जैसे if else statements, switch case के बारे में विस्तृत जानकारी प्राप्त की |

आज के इस पोस्ट Constructors in Java in Hindi  में आप जानेंगे कि, Constructors क्या है , need of constructors तथा उसके कौन-कौन से types है | Java Programming में इनका क्या importance है, इन्हें program में किस प्रकार utilize करें |

दोस्तों, जैसा की आप ने Class and Object से जाना कि Class एक set of objects में उपलब्ध, properties और actions को परिभाषित (define) करता है, जिसे programmer द्वारा code में लिखा जाता है | यहाँ properties को variables तथा actions को methods के रूप में represent किया जाता है |

सरल शब्दों में कहें तो, class एक Template/ model अथवा ढाँचे/साँचा की तरह  कार्य करता है, जिसे जरुरत के अनुसार object create करके use किया जाता है |

चूँकि object, Class से ही create किया जाता है , इसलिए class में available सारे properties और actions, object में भी available होंगे |

ऐसे variables को Instance Variable भी कहते हैं क्योकि ये objects (instance) के अंदर create होते हैं |

यह एक programmer की जिम्मेदारी है कि वो instance variables को requirement के अनुसार initialize करे | इसी initialization में constructor एक important role निभाता है |

Constructors in Java

Constructors का use instance variables को initialize करने के लिए होता है |

Methods की तरह ही constructor में भी बहुत सारे statements होते हैं, जो object creation के दौरान ही execute होते हैं |

जब-जब हम new keyword का use करके object create करते हैं, तब-तब कम से कम एक constructor जरूर call होता है |

Need of Constructors in Java

आइए इसे अलग-अलग programming examples द्वारा समझें |

Program1:

class Employee {
	String name;
	int id;

	void details() {
		System.out.println("My name is " + name);
		System.out.println("My id is " + id);
	}
}

public class ConstructorDemo {
	public static void main(String[] args) {
		Employee ram = new Employee();//creating object of class Employee
		ram.details();	// calling method using object
	}

}

Output:
My name is null
My id is 0

ऊपर लिखे program1, class Employee में,

String name तथा int id, दोनों ही instance variable है, जिन्हे हमने अभी तक initialize नहीं किया है |

Variables को कैसे initialize करें ?

details() एक method है, जो name और id को print करेगा |

class ConstructorDemo में,

Employee ram = new Employee();  इस statement द्वारा हमने object ram create किया है |

ram.details द्वारा हमने details() method को call किया है |

चूँकि, हमने अभी तक String name तथा int id को initialize नहीं किया है | इसीलिए आउटपुट में name की जगह पर null तथा id की जगह 0 मिला, जो कि क्रमशः String तथा int के default values हैं |

Data Types के default values को जानें |

अब ऊपर लिखे Program1 में, class ConstructorDemo को नीचे लिखे code से replace करें |

public class ConstructorDemo {
	public static void main(String[] args) {
		Employee ram = new Employee(); // creating object of class Employee
		ram.name = "Ram"; // initializing instance variable name
		ram.id = 77; // initializing instance variable id
		ram.details(); // calling method using object
	}
}

Output:
My name is Ram
My id is 77

ऊपर लिखे code में, हमने insatance variables String name, int id को object का use करके initialized कर दिया है, इसलिए हमें सही output मिला |

NOTE: यहाँ instance variables, class Employee के अंदर declared हैं, और हमने उन्हें class ConstructorDemo में initialize किया है | यहाँ code की security को neglect (उपेछित) किया जा रहा है | मान लीजिए,

class Employee और class ConstructorDemo दो अलग-अलग programmer द्वारा लिखे गए हैं, इस case में हम किसी और के द्वारा declared class variable को access करें और initialize करें, यह पहले से declared variable के values को overwrite कर देगा जो कि बिल्कुल safe (सुरक्षित) नहीं है |

उदहारण के लिए, class Employee में name और id पहले से “RajHindiMe” तथा 88 द्वारा initialized है और कोई दूसरा programmer उसी variables को  “Ram” और 77 से initialized करता है, इससे output, Ram और 77 ही print होगा, यहाँ data को कोई security नहीं है, इसे नीचे लिखे Program2 में  देखें |

Program2:

class Employee {
	String name = "RajHindiMe";  //initializing instance variable in same class
	int id = 88;

	void details() {
		System.out.println("My name is " + name);
		System.out.println("My id is " + id);
	}
}

public class ConstructorDemo {
	public static void main(String[] args) {
		Employee ram = new Employee(); // creating object of class Employee
		ram.name = "Ram"; // initializing instance variable name
		ram.id = 77; // initializing instance variable id
		ram.details(); // calling method using object
	}
}

Output:
My name is Ram
My id is 77

चूँकि, data का protection बहुत ही important है, तो possibility यह भी हो सकता है कि, class Employee के अन्दर programmer ने उसके instance variables को private declared कर दिया हो |

यहाँ private यह एक Access Modifier है, अधिक जानकारी के लिए, Access Modifier क्या है पेज देखें |

अब इससे यह होगा कि, हम जितनी बार भी class Employee का object create करेंगे, हर बार यह same output ही देगा, नीचे लिखे Program3 में देखें |

Program3:

class Employee {
	private String name = "ram";  //initializing instance variable in same class
	private int id = 77;

	void details() {
		System.out.println("My name is " + name);
		System.out.println("My id is " + id);
	}
}

public class ConstructorDemo {
	public static void main(String[] args) {
		Employee ram = new Employee(); // creating object of class Employee
		ram.details(); // calling method using object
		
	Employee sita = new Employee(); // creating another object of class Employee by another programmer
		sita.details(); // calling method using another object
	}
}

Output:
My name is ram
My id is 77
My name is ram
My id is 77

ऊपर लिखे program3 में हमने दो object create किया है, एक ram और एक sita, परन्तु दोनों के details same ही प्राप्त हुए, जब कि सीता के details में, sita का details ही होना चाहिए था, परन्तु ऐसा नहीं हुआ |

अब तक हमनें, Instance Variables को object name के द्वारा तथा same class में declaration के दौरान ही initialized किया, परन्तु दोनों ही तरीको में कुछ न कुछ limitations (कमी) है | इन्ही कमियों को दूर करने के लिए Constructor अस्तित्व में आया |

Rules for creating constructors in java

1. constructor का name और class का name दोनों हमेशा same होना चाहिए |

2. constructor name के बाद हमेशा () होना चाहिए |

3. constructor में parameters हो भी सकता है, नहीं भी हो सकता |

4. constructor का कोई return type नहीं होता, void भी नहीं |

5. जब हम object create करते हैं, तब constructor हमेशा automatically JVM द्वारा call और execute होता है |

6. constructor प्रत्येक object के लिए एक बार ही call तथा execute होता है |

Types of Constructors in Java

1. Parameterized Constructors

Parameters और कुछ नहीं बल्कि variables होतें हैं, जो outside से data लेकर constructor को देते हैं |

जिस constructor में 1 अथवा 1 से अधिक parameter हों, उसे Parameterized Constructor कहते हैं | यह programmer द्वारा explicitly declare किया जाता है |

2. Non-parameterized Constructors

जिस Constructor में कोई भी parameter नहीं होता उसे Non-parameterized Constructors कहते हैं | यह भी programmer द्वारा explicitly declare किया जाता है |

3. Default Constructor

जब class के अंदर कोई भी constructor define नहीं होता, उस स्थिति में भी object creation के दौरान JVM स्वयं default constructor को call करता है | इस दौरान JVM instance variables को उनके default values द्वारा initialized कर देता है |

NOTE: JVM द्वारा default constructor तभी call होगा, जब class के अन्दर कोई भी constructor न लिखा गया हो |

मान लीजिए, class में केवल parameterized constructor ही लिखा गया हो, और हम object creation के दौरान non-parameterized constructor को call करते हैं, इस स्थिति में JVM कोई भी default constructor नहीं call करेगा बल्कि, हमें compile time error मिलेगा |

How to create constructor and use it

आइए ऊपर लिखे program3 को Non-parameterized constructor का use करके लिखें |

Program4:

class Employee {
	private String name;	
	private int id;
	
	Employee(){		// creating Non-parameterized constructor
		name = "Ram";	// initializing instance variables inside constructor
		id = 77;
	}

	void details() {
		System.out.println("My name is " + name);
		System.out.println("My id is " + id);
	}
}

public class ConstructorDemo {
	public static void main(String[] args) {
		Employee ram = new Employee(); // creating object of class Employee
		ram.details(); // calling method using object
		
		Employee sita = new Employee(); // creating another object of class Employee bya another programmer
		sita.details(); // calling method using another object
	}
}

Output:
My name is Ram
My id is 77
My name is Ram
My id is 77

ऊपर के program4, में भी हमें ram और sita दोनों के लिए same ही output प्राप्त हुआ | क्योकि, यहाँ हमनें non-parameterized constructor के अन्दर केवल ram को ही ध्यान में रख कर ही variables को initialized किया |

इस कमी को हम parameterized constructor का use करके दूर कर सकते हैं, आइए देखें |

Program5:

class Employee {
	private String name;	
	private int id;
	
	Employee(){		// creating Non-parameterized constructor
		name = "Ram";	// initializing instance variables inside constructor
		id = 77;
	}
	Employee(String n, int i){		// creating parameterized constructor with two values
		name = n;	// initializing instance variables inside constructor
		id = i;
	}

	void details() {
		System.out.println("My name is " + name);
		System.out.println("My id is " + id);
	}
}

public class ConstructorDemo {
	public static void main(String[] args) {
		Employee ram = new Employee(); // creating object of class Employee
		ram.details(); // calling method using object
		
		Employee sita = new Employee("Sita", 55); // creating 2nd object of class Employee by another programmer
		sita.details(); // calling method using another object
		
		Employee lakshaman = new Employee("Lakshaman", 99); // creating 3rd object of class Employee by another programmer
		lakshaman.details(); // calling method using another object
	}
}

Output:
My name is Ram
My id is 77
My name is Sita
My id is 55
My name is Lakshaman
My id is 99

ऊपर लिखे program5 में, हमने parameterized constructor का use किया, जिसमें हमने दो variables का use किया |

यहाँ पर हमने ram के लिए object creation के दौरान non-parameterized constructor को तथा sita और lakshaman के लिए object creation के दौरान parameterized constructor को call किया |

यहाँ हमनें, sita के लिए Employee sita = new Employee(“Sita”, 55) तथा lakshaman  के लिए Employee lakshaman = new Employee(“Lakshaman”, 99) का use किया |

इसका अर्थ यह हुआ कि object creation के दौरान हम parameterized constructor में requirements के अनुसार values दे सकते हैं |

NOTE: ऊपर के Program5 में हमने दो constructor create किए हैं, जिनके नाम same हैं परन्तु, वे parameter में भिन्न हैं, इसे ही constructor overloading कहते हैं |

Overloading और Overriding, Polymorphism के parts हैं, जो OOPs Language का important feature है |

Polymorphism को हम आगे आने वाले post में विस्तार से जानेंगे |

Conclusion – आज आपने क्या सीखा

इस post में आपने Constructors तथा उसके types के बारे में विस्तृत जानकारी प्राप्त की |

आशा है कि, आपको मेरा यह Blog Constructor in Java in Hindi जरूर पसंद आया होगा |

अगर आप इस post से related कोई सवाल पूँछना चाहते हैं अथवा कोई सुझाव देना चाहते हैं तो comment करके जरूर बताएं, मैं उसका reply जरूर दूँगा |

इस post को अपना कीमती समय देने के लिए बहुत बहुत धन्यवाद् | फिर मिलेंगें |

2 thoughts on “Constructors in Java in Hindi- Constructors क्या है ?”

    • Hi Sarswati,

      As per my understanding, you are asking “How to create object of a class if it has only private constructor?”

      Ans:
      1. Private constructor does not allow to create object outside the current class, however within a class we can create as many object we need.

      2. Even if we want to create object outside the current class, in that case we must implement the concept of Singleton Class, that we will learn in upcoming post.

      Thank you, keep learning

      Reply

Leave a Comment