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.
-
3:08:55
Laura Loomer
6 hours agoEP125: Mass Deportations NOW!
53.8K47 -
2:36:38
Badlands Media
1 day agoThe Narrative Ep. 25: Kayfabian Chaos
82.6K27 -
7:55:00
SpartakusLIVE
8 hours agoSpecialist EASTER EGG at the Tower of Power || Duos w/ @sophiesnazz, Quads Later?!
86.3K2 -
2:37:59
vivafrei
15 hours agoEp. 267: L.A. Burns AGAIN! Patel on Rogan! Elon's Meltdown! 2nd Amendment Win! SCOTUS & MORE!
125K197 -
2:31:21
TheSaltyCracker
7 hours agoUnleash Rooftop Koreans ReeEEeeStream 06-08-25
126K270 -
2:44:00
IsaiahLCarter
9 hours ago $3.24 earnedAPOSTATE RADIO 017: Ranking Marxist Pluralities, with Seneca Scott
88.8K2 -
1:19:28
Sarah Westall
7 hours agoDeep State Operations, Brain Orgonites, Infowars in the Early Years w/ Jason Bermas
105K18 -
2:37:49
Cavs_Arcade
6 hours ago $0.54 earnedWeekend Vibes! 🍺🍕 | RE4 Remake | #18 |
43.2K -
2:08:10
Nerdrotic
8 hours ago $6.61 earnedMalaysian Flight MH370 and Zero Point Free Energy with Ashton Forbes | Forbidden Frontier #104
75.4K6 -
18:39
Exploring With Nug
1 day ago $6.23 earnedSunken Corvette Recovered From River! Massive Catfish Surprise in the Trunk!
52.2K10