Premium Only Content

#45 This and Super Method in Java | Skyhighes | Lecture 45
Here's a comprehensive explanation of the this and super keywords in Java, focusing on their use within methods:
this Keyword
Within a method, this refers to the current object—the object whose method is being called. It has several uses:
Referencing instance variables: To distinguish between local variables and instance variables with the same name:
Java
public class Person {
private String name;
public void setName(String name) {
this.name = name; // Refers to the instance variable
}
}
Use code with caution. Learn more
Calling other methods of the same object: To invoke another method of the current object:
Java
public class Person {
public void introduce() {
System.out.println("Hello, my name is " + this.getName());
}
}
Use code with caution. Learn more
Passing the current object as an argument: To pass the current object as a reference to another method:
Java
public class Person {
public void register(Event event) {
event.addParticipant(this); // Passing the current Person object
}
}
Use code with caution. Learn more
super Keyword
Within a method, super refers to the immediate parent class of the current object. It's primarily used to:
Access superclass members: To access members (variables or methods) of the superclass that have been hidden or overridden:
Java
public class Employee extends Person {
public String getDetails() {
return super.getName() + ", works in IT"; // Accessing superclass method
}
}
Use code with caution. Learn more
Call superclass constructors: To explicitly call a constructor of the superclass from a subclass constructor:
Java
public class Employee extends Person {
public Employee(String name, String jobTitle) {
super(name); // Calling superclass constructor
this.jobTitle = jobTitle;
}
}
Use code with caution. Learn more
Key Points:
this and super are both reserved keywords in Java.
They are used within methods to refer to different objects within the inheritance hierarchy.
this is used for accessing or calling members of the current object.
super is used for accessing or calling members of the superclass.
Understanding these keywords is essential for working with inheritance and object relationships in Java.
-
1:29:23
Nerdrotic
7 hours ago $8.86 earnedThe Turning Point | New UFO Video with Michael Collins | Forbidden Frontier #117
50.3K23 -
1:08:26
Sarah Westall
5 hours agoSuicide Pacts forming in Youth Social Media Groups - Discord, Reddit, TikTok w/ John Anthony
51.5K14 -
2:25:31
vivafrei
15 hours agoEp. 281: Charlie Kirk; Routh Trial; Charlotte Train; Bolsanaro Defense; SCOTUS & MORE!
126K184 -
2:55:38
Turning Point USA
7 hours agoWASHINGTON D.C. PRAYER VIGIL FOR CHARLIE KIRK
74K31 -
35:54
The Mel K Show
7 hours agoMel K & Tim James | Healing is an Inside Job | 9-14-25
45.8K4 -
3:06:33
IsaiahLCarter
10 hours ago $5.19 earnedCharlie Kirk, American Martyr (with Mikale Olson) || APOSTATE RADIO 028
58.7K7 -
16:43
Mrgunsngear
13 hours ago $4.65 earnedKimber 2K11 Pro Review 🇺🇸
36.9K14 -
13:40
Michael Button
1 day ago $1.89 earnedThe Strangest Theory of Human Evolution
37K17 -
10:19
Blackstone Griddles
1 day agoMahi-Mahi Fish Tacos on the Blackstone Griddle
26.1K3 -
23:51
Jasmin Laine
1 day ago“Stop Wasting My Time!”—Trump's BRUTAL WARNING To Canada As Poilievre ROASTS CBC LIVE
20.1K28