Theory
assignment જે કંઈ કર્યું નહીં
તમે last lesson ની advice લો છો અને constructor ને honest parameter names આપો છો:
Book(String title) { title = title; }
perfectly compile થાય છે. પછી new Book("Java").title null print કરે છે.
તે constructor ની અંદર, name title એ parameter નો અર્થ કરે છે, જે field ને shadow કરે છે. એટલે line parameter ને તેના પોતાની સાથે assign કરે છે, well-formed sentence જે કંઈ કહેતું નથી, અને field એના વિશે ક્યારેય જાણતું નથી. Java નો fix એ pronoun છે: this.
Theory
family dinner પર બે pronouns
જ્યારે table પર 2 લોકો બંને Riya કહેવાતા હોય, તમે point કરો: "this Riya, right here sitting."
this exactly એ gesture છે: current object, જેની method run થઈ રહી છે. અને જ્યારે તમે તમારા own twist ને બદલે તમારા father ના family recipe નો version mention કરો, તમે કહો છો "my parent's one": એ super છે, એક generation ઉપર નો reference.
Theory
this અને super, ઔપચારિક રીતે
this: current object નો reference.
this.title = title;ડાબી બાજુ field, જમણી બાજુ parameter: shadow resolvedthis(...)same class ના sibling constructor ને calls કરે છે (constructor chaining)
super: object ના parent side નો reference.
super.describe()overridden method ના parent version ને run કરે છેsuper(args)parent constructor ને calls કરે છે, અને તે child constructor નું first statement હોવું જોઈએ
Practical
family grammar action માં
class Person {
String name;
Person(String name) {
this.name = name; // this shadow ને untangle કરે છે
}
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 નું 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 શું contain કરે છે?
- "Java"
- null
- Compile થતું નથી: names clash થાય છે
- ખાલી string ""
Show the answer
null
parameter field ને shadow કરે છે, એટલે title = title parameter ને તેના પોતાની સાથે assign કરે છે; field String reference માટે તેનું default, null, retain કરે છે. Option A એ છે જે code LOOKS કરે છે કે તે કરે છે, જે એટલે આ bug code reviews ને survive કરે છે. Option C false છે: shadowing legal Java છે, merely treacherous. Option D null (no object) ને empty string (length 0 ની real String) સાથે confuse કરે છે. એક keyword એને repair કરે છે: this.title = title.
Think first
શા માટે super() first જવું જોઈએ?
Java Member ના constructor ના first statement સિવાય ક્યાંય super(name) ને refuse કરે છે. tap કરતા પહેલા WHY વિશે વિચારો: તેના પહેલા child code run થવાથી શું wrong થઈ શકે?
Show the answer
કારણ કે objects parent-first build થાય છે. Member નું code inherited pieces (name, Person ની methods) ને use કરી શકે છે, એટલે Person layer ને કોઈ Member line run થાય તે પહેલાં fully constructed હોવું જોઈએ. child code ને first 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) ને yourself call ન કરો.
Watch out
જ્યાં this અને super break થાય છે
static context માં this: main static છે; ત્યાં કોઈ current object નથી, એટલે this તેની અંદર compile error છે. (static lesson, next, deeper reason ને explain કરે છે.)
super(...) first નહીં: તેના પહેલા કોઈ પણ statement, even println, compilation ને kill કરે છે.
Chained calls collide: this(...) અને super(...) બંને first line ને demand કરે છે, એટલે એક constructor તેમાંથી at most એક ને use કરી શકે છે.
Theory
same pronouns, other languages
C++ એ this ને actual pointer તરીકે had હતું અને parent classes ને explicitly named કરતું હતું. next semester નું partner subject BCA404 (VB.NET) identical ideas ને Me અને MyBase તરીકે spell કરે છે; એમને different clothes માં this અને super તરીકે recognise કરવાથી એ unit half-learnt already બને છે. BookBridge ની અંદર, આ pair constructor story ને complete કરે છે: આ semester નો દરેક remaining class this સાથે initialise થાય છે અને super સાથે upward hand કરે છે.
Summary
Key takeaways
- this = current object; this.field = parameter એ shadowed names માટે standard cure છે.
- title = title parameter ને તેના પોતાની સાથે assign કરે છે; field null રહે છે. તેના પર watch રાખો.
- super.method() parent નું version run કરે છે; super(args) parent constructor ને run કરે છે.
- super(...) first 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 અહીં points કરે છે, super ઉપર points કરે છે.