this and super Keywords in Java
In this lesson: Use this and super keywords in Java — fields, methods, constructors — difference table and examples with output. In Hindi & English.
What are this and super
this and super are two special keywords. this refers to the current object, and super refers to the parent class. They help you access the right field, method or constructor, especially when names clash between a class and its parent.
this — the current object
class Student {
String name;
void setName(String name) {
this.name = name; // this.name = field, name = parameter
}
}
When a parameter has the same name as a field, name means the parameter and this.name means the object's field. this clears up the ambiguity — very common in setters and constructors.
this() for constructors
class Box {
int w, h;
Box() { this(1, 1); } // call the other constructor
Box(int w, int h) {
this.w = w; this.h = h;
}
}
this() calls another constructor in the same class, reusing its setup. It must be the first statement in the constructor.
super — the parent
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
void sound() {
super.sound(); // call parent's version
System.out.println("Woof");
}
}
Woof
super.sound() calls the parent's method even though the child overrode it. This lets a child add to the parent's behaviour instead of fully replacing it.
super() in constructors
class Person {
String name;
Person(String name) { this.name = name; }
}
class Student extends Person {
int roll;
Student(String name, int roll) {
super(name); // call parent constructor
this.roll = roll;
}
}
super(name) runs the parent's constructor first, so the parent sets up its part (name), then the child adds its own (roll). It must be the first line of the constructor.
this vs super
| Point | this | super |
|---|---|---|
| Refers to | Current object | Parent class |
| Field access | this.x | super.x |
| Method call | this.m() | super.m() |
| Constructor | this(...) | super(...) |
Common mistakes
- Forgetting
thiswhen names clash.name = name;assigns the parameter to itself. super()not first. It must be the first statement in the child constructor.- Using both this() and super(). Only one can be first, so you cannot use both.
- Expecting super without inheritance.
superonly makes sense in a subclass.
Summary
thisrefers to the current object;superto the parent class.- Use
this.field = fieldwhen a parameter shares a field's name. this()calls another constructor in the same class.super.method()calls the parent's version;super(...)its constructor.- Both
this()andsuper()must be the first line, so use only one.
Next, see them in action with inheritance, or set up objects with constructors.
Frequently Asked Questions
What does the this keyword do in Java?
this refers to the current object — the one a method or constructor is working on. It is most useful to tell apart a field from a parameter with the same name, like this.name = name;.What does the super keyword do?
super refers to the parent (superclass). You use it to call the parent's constructor with super(...), or to access a parent field or method that a child has overridden, like super.show().Why use this.name = name in a constructor?
name alone means the parameter. this.name means the object's field, so this.name = name; stores the parameter into the field clearly.What is the difference between this() and super()?
this() calls another constructor in the same class. super() calls a constructor in the parent class. Both must be the first statement in a constructor, so you cannot use both together.When is super() called automatically?
super() yourself, Java automatically calls the parent's no-argument constructor first. You only write super(...) explicitly when you need to pass values to a parent constructor.