Theory
The assignment that did nothing
You take last lesson's advice and give the constructor honest parameter names:
Book(String title) { title = title; }
Compiles perfectly. Then new Book("Java").title prints null.
Inside that constructor, the name title means the parameter, which shadows the field. So the line assigns the parameter to itself, a well-formed sentence that says nothing, and the field never hears about it. Java's fix is a pronoun: this.
Theory
Two pronouns at a family dinner
When 2 people at the table are both called Riya, you point: "this Riya, sitting right here."
this is exactly that gesture: the current object, the one whose method is running. And when you mention your father's version of a family recipe rather than your own twist on it, you say "my parent's one": that is super, the reference up one generation.
Theory
this and super, formally
this: a reference to the current object.
this.title = title;field on the left, parameter on the right: shadow resolvedthis(...)calls a sibling constructor of the same class (constructor chaining)
super: a reference to the parent's side of the object.
super.describe()runs the parent's version of an overridden methodsuper(args)calls the parent constructor, and it must be the first statement of the child constructor
Practical
The family grammar in 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; } After new Book("Java"), what does the field title contain?
- "Java"
- null
- It does not compile: the names clash
- An empty string ""
Show the answer
null
The parameter shadows the field, so title = title assigns the parameter to itself; the field keeps its default for a String reference, null. Option A is what the code LOOKS like it does, which is why this bug survives code reviews. Option C is false: shadowing is legal Java, merely treacherous. Option D confuses null (no object) with an empty string (a real String of length 0). One keyword repairs it: this.title = title.
Think first
Why must super() go first?
Java refuses super(name) anywhere except the first statement of Member's constructor. Think of WHY before tapping: what could go wrong with child code running before it?
Show the answer
Because objects are built parent-first. Member's code may use inherited pieces (name, Person's methods), so the Person layer must be fully constructed before any Member line runs. Allowing child code first would let it touch a half-built parent. Bonus fact: write no super(...) at all and Java silently inserts a no-arg super(); if the parent has no no-arg constructor (our Person does not), that insertion fails and the child does not compile until you call super(name) yourself.
Watch out
Where this and super break
this in static context: main is static; there is no current object there, so this is a compile error inside it. (The static lesson, next, explains the deeper reason.)
super(...) not first: any statement before it, even a println, kills compilation.
Chained calls collide: this(...) and super(...) both demand the first line, so one constructor can use at most one of them.
Theory
The same pronouns, other languages
C++ had this as an actual pointer and named parent classes explicitly. Next semester's partner subject BCA404 (VB.NET) spells the identical ideas Me and MyBase; recognising them as this and super in different clothes makes that unit half-learnt already. Within BookBridge, this pair completes the constructor story: every remaining class this semester initialises itself with this and hands upward with super.
Summary
Key takeaways
- this = the current object; this.field = parameter is the standard cure for shadowed names.
- title = title assigns a parameter to itself; the field stays null. Watch for it.
- super.method() runs the parent's version; super(args) runs the parent constructor.
- super(...) must be the first statement; omitted, Java inserts no-arg super(), which needs the parent to have one.
- Objects construct parent-first; that ordering is why the first-line rule exists.
- this is illegal in static context; this() and super() cannot share a constructor.
- Memory hook: this points here, super points up.