Theory
वह Assignment जिसने कुछ नहीं किया
आप पिछले lesson की advice लेते हैं और constructor को honest parameter names देते हैं:
Book(String title) { title = title; }
Perfectly compile होता है। फिर new Book("Java").title null print करता है।
उस constructor के अंदर, नाम title का मतलब है parameter, जो field को shadow करता है। तो line parameter को खुद को assign करती है, एक well-formed sentence जो कुछ नहीं कहता, और field को इसके बारे में कभी पता नहीं चलता। Java का fix एक pronoun है: this।
Theory
Family Dinner पर दो Pronouns
जब table पर 2 लोग दोनों Riya कहलाते हैं, आप point करते हैं: "यह Riya, यहाँ बैठी हुई।"
this exactly वह gesture है: current object, जिसका method run हो रहा है। और जब आप अपनी twist के बजाय अपने father की family recipe की version mention करते हैं, आप कहते हैं "मेरे parent का वाला": यह super है, एक generation ऊपर का reference।
Theory
this और super, Formally
this: current object का एक reference।
this.title = title;बाएँ field, दाएँ parameter: shadow resolve हो गयाthis(...)same class का एक sibling constructor call करता है (constructor chaining)
super: object के parent side का एक reference।
super.describe()एक overridden method का parent's version run करता हैsuper(args)parent constructor call करता है, और यह child constructor का पहला statement होना चाहिए
Practical
Family Grammar Action में
class Person {
String name;
Person(String name) {
this.name = name; // this untangles the shadow
}
void describe() {
System.out.println("Person: " + name);
}
}
class Member extends Person {
int booksIssued;
Member(String name, int booksIssued) {
super(name); // parent constructor, FIRST line
this.booksIssued = booksIssued;
}
@Override
void describe() {
super.describe(); // parent's version first...
System.out.println("Books issued: " + booksIssued);
}
}
public class BookBridge {
public static void main(String[] args) {
new Member("Riya", 2).describe();
}
}
Quiz
Book(String title) { title = title; } new Book("Java") के बाद, field title में क्या है?
- "Java"
- null
- यह compile नहीं होता: names clash करते हैं
- एक empty string ""
Show the answer
null
Parameter field को shadow करता है, तो title = title parameter को खुद को assign करता है; field अपने String reference के लिए default रखता है, null। Option A यह है कि code क्या करता है ऐसा LOOK होता है, यही वजह है यह bug code reviews में survive करता है। Option C ग़लत है: shadowing legal Java है, बस treacherous। Option D null (कोई object नहीं) को एक empty string (length 0 का एक real String) से confuse करता है। एक keyword इसे repair करता है: this.title = title।
Think first
super() पहले क्यों जाना चाहिए?
Java Member के constructor के पहले statement के अलावा कहीं भी super(name) से मना करता है। Tap करने से पहले WHY सोचिए: इससे पहले child code run होने में क्या ग़लत हो सकता है?
Show the answer
क्योंकि objects parent-first built होते हैं। Member का code inherited pieces (name, Person के methods) इस्तेमाल कर सकता है, तो किसी भी Member line run होने से पहले Person layer पूरी तरह constructed होनी चाहिए। पहले child code allow करने से यह एक half-built parent को touch कर सकता है। Bonus fact: बिल्कुल super(...) मत लिखिए और Java silently एक no-arg super() insert करता है; अगर parent के पास कोई no-arg constructor नहीं है (हमारे Person के पास नहीं है), वह insertion fail होती है और child तब तक compile नहीं होता जब तक आप खुद super(name) call न करें।
Watch out
जहाँ this और super टूटते हैं
Static Context में this: main static है; वहाँ कोई current object नहीं है, तो this इसके अंदर एक compile error है। (Static lesson, अगला, deeper reason explain करता है।)
super(...) पहले नहीं: इससे पहले कोई भी statement, एक println भी, compilation kill कर देता है।
Chained Calls Collide करते हैं: this(...) और super(...) दोनों first line demand करते हैं, तो एक constructor इनमें से अधिकतम एक इस्तेमाल कर सकता है।
Theory
वही Pronouns, दूसरी Languages
C++ में this एक actual pointer था और parent classes explicitly named थीं। अगले semester की partner subject BCA404 (VB.NET) identical ideas को Me और MyBase spell करती है; उन्हें different clothes में this और super की तरह recognise करना उस unit को आधा पहले से सीखा हुआ बना देता है। BookBridge के अंदर, यह pair constructor story पूरी करती है: इस semester की बाकी हर class this से खुद को initialise करती है और super से ऊपर hand करती है।
Summary
Key takeaways
- this = current object; this.field = parameter shadowed names का standard cure है।
- title = title एक parameter को खुद को assign करता है; field null रहती है। इसे watch कीजिए।
- super.method() parent's version run करता है; super(args) parent constructor run करता है।
- super(...) पहला statement होना चाहिए; omitted होने पर, Java no-arg super() insert करता है, जिसे parent के पास एक चाहिए।
- Objects parent-first construct होते हैं; यही ordering वजह है first-line rule की।
- this static context में illegal है; this() और super() एक constructor share नहीं कर सकते।
- Memory hook: this यहाँ point करता है, super ऊपर point करता है।