Theory
वह Book जो Broken Born हुई
एक नया intern BookBridge में 20 books add करता है। Book 14 पर वह title set करता है, distracted हो जाता है, और कभी copies set नहीं करता।
Object exist करता है। इसकी copies field default hold करती है, 0। Weeks बाद एक student इसे borrow करने की कोशिश करता है, issue() कहता है no stock, और वह एक physical copy एक semester तक shelf पर unissued बैठी रहती है।
किसी ने एक ग़लत value नहीं लिखी; किसी ने बस भूल गया। Fix यह है भूलना impossible बनाना: कोई Book अपने facts state किए बिना create नहीं होता।
Theory
Admission Desk
एक college एक student को wander in करके form "जब भी" fill करने नहीं देता। Admission एक desk पर होता है: name और course entry पर लिए जाते हैं, या कोई admission नहीं है।
एक constructor objects के लिए वह desk है। यह new के moment पर automatically run होता है, facts को parameters की तरह receive करता है, और तभी object hand over करता है। कुछ भी system में half-registered enter नहीं होता।
Theory
Constructor, Formally
एक constructor एक special member है जो एक नया object initialise करता है। Exam check करने वाले rules:
- इसका नाम exactly class name है
- इसका कोई return type नहीं, void भी नहीं
- यह
newexecute होने पर automatically run होता है; आप इसे कभी method की तरह call नहीं करते - यह overloaded हो सकता है: अलग parameter lists वाले कई constructors
Book(String t, int c) { title = t; copies = c; }
अब new Book("Let Us C", 5) एक move में एक complete book बनाता है।
Practical
Born Complete, 2 तरीकों से
class Book {
String title;
int copies;
Book(String t, int c) { // parameterized constructor
title = t;
copies = c;
}
Book() { // no-arg constructor, written by US
title = "Untitled";
copies = 1;
}
}
public class BookBridge {
public static void main(String[] args) {
Book b1 = new Book("Let Us C", 5);
Book b2 = new Book(); // works because we wrote Book()
System.out.println(b1.title + " | " + b1.copies);
System.out.println(b2.title + " | " + b2.copies);
}
}
Theory
वह Vanishing Default Constructor
जब Book के पास बिल्कुल कोई constructor नहीं था, तब new Book() कहाँ से काम करता था? Compiler ने एक default constructor supply किया: invisible, कोई arguments नहीं, fields अपने defaults पर छोड़े हुए।
Catch: वह gift सिर्फ़ तब तक exist करता है जब तक आप अपना कोई constructor define नहीं करते। जिस moment आप Book(String t, int c) लिखते हैं, free वाला vanish हो जाता है। Program में कोई भी old new Book() instantly एक compile error बन जाता है, जब तक आप खुद भी एक no-arg constructor न लिखें, जैसे listing करती है।
Quiz
class Book { Book(String t) { title = t; } } कहीं और कोई लिखता है: new Book(); क्या होता है?
- Compile error: अब कोई matching constructor exist नहीं करता
- यह काम करता है; compiler का default constructor हमेशा available है
- यह काम करता है; fields बस अपनी default values रखते हैं
- पहली बार missing constructor call होने पर runtime exception
Show the answer
Compile error: अब कोई matching constructor exist नहीं करता
String constructor define करना compiler के free no-arg वाले को हटा देता है, तो new Book() के पास match करने के लिए कुछ नहीं है और compilation fail होती है। Options B और C उस world को describe करते हैं जो किसी constructor के define होने से PEHLE था; वह world आपके पहले constructor से खत्म हो जाता है। Option D impossible है: constructor matching पूरी तरह compile time पर settle होता है। यह vanishing-default rule इस paper में सबसे ज़्यादा पूछा जाने वाला constructor question है।
Think first
Imposter Constructor
एक student class Book के अंदर लिखता है: void Book() { title = "X"; } और confused है कि new Book() उसकी debug line कभी print क्यों नहीं करता। Tap करने से पहले: उसने असल में क्या create किया?
Show the answer
एक ordinary method जिसका नाम accidentally Book है। जिस moment एक return type appear होता है (void भी), यह एक constructor नहीं है, तो new Book() कभी इसे call नहीं करता; यह quietly एक constructor इस्तेमाल करता है। उसका method सिर्फ़ तभी run होता अगर कोई explicitly b.Book() लिखे, जो कोई नहीं करता। Exam rule: class name + कोई return type नहीं = constructor; बाकी सब कुछ बस एक method है।
Watch out
और Destructor?
BCA304 reflex: constructor lesson के बाद destructor lesson आता है। Java में नहीं। कोई destructors नहीं हैं; garbage collector unreachable objects reclaim करता है (comparison lesson का locked-drawer trade)।
अगर एक comparison question पूछे, लिखिए: C++ manual cleanup के लिए constructors को destructors के साथ pair करता है; Java constructors को automatic garbage collection के साथ pair करता है। एक Java destructor syntax invent मत कीजिए; लिखने के लिए कोई नहीं है।
Theory
एक Loose Thread
हमारे constructor के parameters देखिए: Book(String t, int c)। Cryptic t और c क्यों? क्योंकि उन्हें title और copies naming करना, natural choice, fields के साथ एक name clash create करता है, और उस clash को properly solve करने के लिए this keyword चाहिए। यही exactly अगला lesson है, जहाँ BookBridge की Person family भी super के साथ parent constructors call करना शुरू करती है। Admission desk को एक hierarchy मिलने वाली है।
Summary
Key takeaways
- एक constructor new पर object initialise करता है: class name, कोई return type नहीं, कभी manually call नहीं किया जाता।
- Parameterized constructors incomplete objects को impossible बनाते हैं: facts birth पर arrive करते हैं।
- Compiler का default constructor सिर्फ़ तब तक exist करता है जब तक आप कोई constructor define नहीं करते; आपका पहला वाला इसे हटा देता है।
- Constructors overload होते हैं: कई parameter lists, एक class name।
- void Book() एक method है, constructor नहीं: कोई भी return type इसे disqualify करता है।
- Java के पास constructors हैं पर destructors नहीं; garbage collection life के दूसरे end को handle करता है।
- Memory hook: desk के बिना कोई admission नहीं।