Skip to content

Commit 363c6b1

Browse files
committed
feat: Difference Between this and super in Java.
Signed-off-by: Somesh diwan <[email protected]>
1 parent 4cbfb15 commit 363c6b1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Section10Methods/LocalandGlobalVariables/src/Difference Between `this` and `super` in Java.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Difference Between `this` and `super` in Java
22

3-
Both `this` and `super` are keywords in Java that help in referring to the current and parent class objects, respectively. Below is a detailed comparison:
3+
Both `this` and `super` are keywords in Java that help in referring to the current and parent class objects, respectively.
4+
5+
Below is a detailed comparison:
46

57
| Feature | `this` | `super` |
68
|------------------------|----------------------------------------------------------------------|-------------------------------------------------------------------|
@@ -11,7 +13,7 @@ Both `this` and `super` are keywords in Java that help in referring to the curre
1113
| Method Overriding | Used to call the current class method explicitly | Used to call a superclass method when it is overridden |
1214
| Cannot be Used In | Static methods | Static methods |
1315
| Example | `this.variableName` (access current class variables) | `super.variableName` (access parent class variables) |
14-
---
16+
1517

1618
Examples:
1719

@@ -35,7 +37,7 @@ public class TestThis {
3537
obj.show();
3638
}
3739
}
38-
```
40+
3941
✅ this.x = x; avoids confusion between local and instance variables.
4042

4143

@@ -60,7 +62,7 @@ public class TestSuper {
6062
obj.display();
6163
}
6264
}
63-
```
65+
6466
✅ `super.x` refers to the parent class variable, while `this.x` refers to the child class variable.
6567

6668
---
@@ -119,8 +121,8 @@ public class TestSuperConstructor {
119121

120122
`super();` ensures that the parent constructor is executed before the child constructor.
121123

122-
Key Takeaways
124+
Key Takeaways:
123125

124126
1. `this` refers to the current class, while `super` refers to the parent class.
125127
2. `this()` calls another constructor in the same class, while `super()` calls the parent class constructor.
126-
3. Use `this` to differentiate instance variables from parameters and `super` to access overridden methods or hidden variables.
128+
3. Use `this` to differentiate instance variables from parameters and `super` to access overridden methods or hidden variables.

0 commit comments

Comments
 (0)