Constructor in Java

Constructor in Java: दुनिया का कोई भी Real World Object ऐसा नहीं होता है, जिसके विभिन्न Attributes का Object के Create होते समय कोई मान ना हो और सभी मान बाद में Set किए जाते हों। यानी मानलो कि कोई Company एक cpuCabinet बनाती है। तो Company कोई भी cpuCabinet ऐसा नहीं बना सकती जिसके निर्माण के समय उस cpuCabinet Object का कोई Length, width व Height ना हो। जो भी cpuCabinet Company बनाएगी उस हर cpuCabinet का कोई ना कोई प्रारम्भिक Dimension जरूर होगा।

इसी Concept के आधार पर जावा में भी ये सुविधा प्रदान की गई है कि किसी भी Class का कोई भी Object Create होते समय उसमें प्रारम्भिक मान Initialized हो सके। Create होने वाले Object में Object के Create होते समय ही कुछ प्रारम्भिक मान Initialize करने के लिए एक Method Create किया जाता है। इस Method को Constructors कहते हैं।

Constructor किसी Class का एक ऐसा Member Method होता है, जिसका नाम वही होता है जो उस Class का नाम होता है, जिसमें ये Member Method होता है। उदाहरण के लिए यदि हम Box Class का Constructor Define करना चाहें तो ये Constructor Box Class का एक Member Method होगा और इस Method का नाम भी Box() ही होगा।

चूंकि Constructor का वही नाम होता है जो Class का नाम होता है, इसलिए जब भी हम उस Class का कोई Object Create करते हैं, तो Object के Create होते ही ये Constructor Method Execute हो जाता है। इसलिए हम इस Method में किसी Object को प्रदान किए जाने वाले प्रारम्भिक मान प्रदान कर देते हैं और जैसे ही हम किसी Class का कोई Object Create करते हैं, उस Class का ये Constructor Execute हो कर Create होने वाले Object के विभिन्न Instance Variables को Initialize कर देता है।

Constructors की जरूरत हमें इसलिए भी पडती है क्योंकि जब भी कोई Object Create होता है तो उसका कोई ना कोई प्रारंभिक मान भी जरूर होता है। इसलिए यदि हम Constructors का प्रयोग नहीं करते हैं, तो Object Create होने के बाद हमें उस Object के विभिन्न Instance Variables को विभिन्न मान Initialize करने पडते हैं। इस काम को करने के लिए हम एक Initialize() नाम का Member Method भी Create कर सकते हैं, लेकिन ऐसा करने पर हर Object के लिए इस Method को Call करना पडता है।

// Program
	class Box
	{
		//Attributes
		double width;
		double height;
		double length;

		//Methods
		public double volume()
		{
			double volume;
			volume = this.length * this.height * this.width;
			return volume;
		}
		public void initializeObject(double len, double wid, double hei)
		{
			width = wid;
			length = len;
			height = hei;
		}
	}
	
	public class BoxVolumeWithBoxObjectArgument
	{
		public static void main(String args[])
		{
			Box cpuCabinet = new Box();
			Box hardDisk = new Box();
			
			double volumeCPUCabinet;
			double volumeHardDisk;
			
			cpuCabinet.initializeObject(1, 2, 3);
			hardDisk.initializeObject(1, 2, 3);
			
			volumeCPUCabinet = cpuCabinet.volume();
			System.out.println("Volume of CPU Cabinet is " + volumeCPUCabinet);
	
			volumeHardDisk = hardDisk.volume();
			System.out.println("Volume of Hard Disk is " + volumeHardDisk);
		}
	}

// Output
   Volume of CPU Cabinet is 6.0
   Volume of Hard Disk is 6.0

इस उदाहरण में हमने एक initializeObject नाम का Member Method Create किया है। Object को Create करने के बाद हर Object के लिए हमें इस Method को Call करके उस Object को Initialize करना जरूरी होता है। अब इसी Program को हम Constructor के प्रयोग द्वारा निम्नानुसार Modify कर सकते हैं-

// Program
	class Box
	{
		//Attributes
		double width;
		double height;
		double length;
		
		//Methods
		public double volume()
		{
			double volume;
			volume = length * height * width;
			return volume;
		}
		
		Box()		//No-Argument Constructor
		{
			System.out.println("No-Argument Constructor Executed");
			width =1;
			length =2;
			height =3;
		} 
	}
	
	public class ConstructorDemo
	{
		public static void main(String args[])
		{
			Box cpuCabinet = new Box();
			Box hardDisk = new Box();
			
			double volumeCPUCabinet;
			double volumeHardDisk;
			
			volumeCPUCabinet = cpuCabinet.volume();
			System.out.println("Volume of CPU Cabinet is " + volumeCPUCabinet);
	
			volumeHardDisk = hardDisk.volume();
			System.out.println("Volume of Hard Disk is " + volumeHardDisk);
		}
	}

// Output 
   No-Argument Constructor Executed
   No-Argument Constructor Executed
   Volume of CPU Cabinet is 6.0
   Volume of Hard Disk is 6.0

इन दोनों उदाहरणों के Output समान ही हैं जो कि हम देख सकते हैं, लेकिन main() Method में अन्तर है। पहले Program में एक initializeObject() Method को Object के लिए Call करके Object के विभिन्न Instance Variables को Initialize किया गया है, जबकि दूसरे Program में ये काम Java Compiler Constructor का प्रयोग करके स्वयं ही कर लेता है।

जब हम Box Class का एक Object cpuCabinet Create करते हैं, तो जैसे ही Compiler नए Object के लिए Memory Allocate कर देता है, उस Memory Locations पर विभिन्न Instance Variables के लिए मान भी Initialize हो जाते हैं।

ऐसा इसलिए होता है, क्योंकि Object के लिए Memory Allocate होने के तुरन्त बाद Box Class का Box नाम का Constructor Method Execute हो जाता है और वह Method Object के विभिन्न Instance Variables को Initialize कर देता है।

इसी तरह से जब दूसरा Object Create होता है तो दूसरे Object का भी पहले Constructor Execute होता है और Object के विभिन्न Dimensions को Initialize कर देता है। Output में हम प्राप्त होने वाली पहली दो Lines के आधार पर हम समझ सकते हैं, कि Box Constructor Execute हो रहा है।

जब हम Constructors Define कर रहे होते हैं, तब हमें एक बात का ध्‍यान रखना चाहि, कि Constructor Method कभी भी कोई मान Return नहीं करता है, इसलिए इसमें return Statement का प्रयोग नहीं किया जाना चाहि, साथ ही Return Type को भी Declare नहीं किया जाना चाहिए। यहां तक कि Return Type के स्थान पर void का भी प्रयोग करने पर Compiler Error प्रदान करता है।

साथ ही एक Class का Constructor केवल उसी Class के Object के लिए Execute होता है, इसलिए इसके साथ किसी Access Specifier का भी प्रयोग नहीं किया जाना होता है। यानी किसी Class का Constructor Method Create करते समय हमें उस Class के नाम को ही Constructor के नाम के रूप में Define करना होता है।

इसीलिए हमने हमारे Program में केवल निम्नानुसार Statement द्वारा ही Constructor को Define किया है, जिसमें किसी Access Specifier या Return Type का प्रयोग नहीं किया गया है:

        Box()                             //No-Argument Constructor

चूंकि, ये Constructor किसी प्रकार का कोई मान main() Method से प्राप्त नहीं करता है, इसलिए इसके Parenthesis को Empty रखा गया है। जब किसी Constructor का Parenthesis Empty होता है तो इस प्रकार के Constructor को No-Argument Constructor कहा जाता है।

हालांकि हम इस तरह से Initialization कर सकते हैं, लेकिन किसी Object को Create करते समय ही यदि उसके विभिन्न Dimensions को Set किया जा सके, तो ज्यादा बेहतर होता है, क्योंकि ऐसा जरूरी नहीं होता है कि Create होने वाले सभी Objects के Attributes के मान हमेंशा समान ही हों। यानी हम Box Class से जितने भी CPU Box Object Create करते हैं, हमेंशा ऐसा होना जरूरी नहीं होता है, कि Create होने वाले हर CPU की Height, Width व Length समान हो।

इसलिए एक ही Class के कई अलग-अलग Size के Box Objects Create करने के लिए हम ऐसा Constructor Method Define कर सकते हैं, जिसका प्रयोग करके Create होने वाले हर Object की Size को Object Creation के समय ही Define किया जा सके। इस प्रकार के Constructor को Parameterized Constructor कहा जाता है।

चूंकि जावा में हम Methods की Overloading कर सकते हैं, इसलिए हम Constructors की भी Overloading कर सकते हैं। यानी हम एक ही नाम के एक से अधिक Constructors को एक ही Class में Define कर सकते हैं।

// Program
	class Box
	{
		//Attributes
		double width;
		double height;
		double length;
		
		//Methods
		public double volume()
		{
			double volume;
			volume = length * height * width;
			return volume;
		}
		
		Box()		//No-Argument Constructor
		{
			System.out.println("No-Argument Constructor Executed");
			width =1;
			length =2;
			height =3;
		} 
		
		Box(double len, double wid, double hei)
		{
			System.out.println("3-Argument Constructor Executed");
			width = wid;
			length = len;
			height = hei;
		}
	}
	
	public class ConstructorDemo
	{
		public static void main(String args[])
		{
			Box cpuCabinet = new Box();
			Box hardDisk = new Box(1,2,3);
			
			double volumeCPUCabinet;
			double volumeHardDisk;
			
		
			volumeCPUCabinet = cpuCabinet.volume();
			System.out.println("Volume of CPU Cabinet is " + volumeCPUCabinet);
	
			volumeHardDisk = hardDisk.volume();
			System.out.println("Volume of Hard Disk is " + volumeHardDisk);
		}
	}

// Output 
   No-Argument Constructor Executed
   3-Argument Constructor Executed
   Volume of CPU Cabinet is 6.0
   Volume of Hard Disk is 6.0

इस Program के Output से हम समझ सकते हैं कि जब हम cpuCabinet नाम का Box Object Create करते हैं, तब Box Class का No-Argument Constructor Execute होता है और जब हम Box Class का hardDisk Object Create करते हैं और उसके Constructor में Dimension के तीनों मान Define कर देते हैं, तो Compiler hardDisk Object के लिए 3-Argument Constructor को Execute करता है, जैसाकि हम Output में देख सकते हैं और hardDisk Object को Initialized कर देता है।

Java Programming Language in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Java in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

Java Programming Language in Hindi | Page: 682 | Format: PDF

BUY NOW GET DEMO REVIEWS