|
| 1 | +# this and super keyword in Java |
| 2 | + |
| 3 | +### this keyword in Java : |
| 4 | +- this is a way for us to reference an object of the class which is being created/referenced. |
| 5 | +- It is used to call the default constructor of the same class. |
| 6 | +- this keyword eliminates the confusion between the parameters and the class attributes with the same name. Take a look at the example given below : |
| 7 | + |
| 8 | +``` |
| 9 | +class cwh{ |
| 10 | + int x; |
| 11 | +
|
| 12 | +// getter of x |
| 13 | + public int getX(){ |
| 14 | + return x; |
| 15 | + } |
| 16 | +
|
| 17 | + // Constructor with a parameter |
| 18 | + cwh(int x) { |
| 19 | + x = x; |
| 20 | + } |
| 21 | +
|
| 22 | + // Call the constructor |
| 23 | + public static void main(String[] args) { |
| 24 | + cwh obj1 = new cwh(65); |
| 25 | + System.out.println(obj1.getX()); |
| 26 | +
|
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +``` |
| 32 | +Output : |
| 33 | +0 |
| 34 | +``` |
| 35 | + |
| 36 | +- In the above example, the expected output is 65 because we've passed x=65 to the constructor of the cwh class. But the compiler fails to differentiate between the parameter 'x' & class attribute 'x.' Therefore, it returns 0. |
| 37 | + |
| 38 | +- Now, let's see how we can handle this situation with the help of this keyword. Take a look at the below code : |
| 39 | + |
| 40 | +``` |
| 41 | +class cwh1{ |
| 42 | + int x; |
| 43 | +
|
| 44 | +// getter of x |
| 45 | + public int getX(){ |
| 46 | + return x; |
| 47 | + } |
| 48 | +
|
| 49 | + // Constructor with a parameter |
| 50 | + cwh(int x) { |
| 51 | + this.x = x; |
| 52 | + } |
| 53 | +
|
| 54 | + // Call the constructor |
| 55 | + public static void main(String[] args) { |
| 56 | + cwh obj1 = new cwh(65); |
| 57 | + System.out.println(obj1.getX()); |
| 58 | +
|
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +``` |
| 64 | +Output : |
| 65 | +65 |
| 66 | +``` |
| 67 | + |
| 68 | +- Now, you can see that we've got the desired output |
| 69 | + |
| 70 | +### Super keyword |
| 71 | +- A reference variable used to refer immediate parent class object. |
| 72 | +- It can be used to refer immediate parent class instance variable. |
| 73 | +- It can be used to invoke the parent class method. |
| 74 | + |
| 75 | + |
| 76 | +### Code as described in the video: |
| 77 | + |
| 78 | +``` |
| 79 | +import javax.print.Doc; |
| 80 | +class EkClass{ |
| 81 | + int a; |
| 82 | + public int getA() { |
| 83 | + return a; |
| 84 | + } |
| 85 | + |
| 86 | + EkClass(int a) { |
| 87 | + this.a = a; |
| 88 | + } |
| 89 | + |
| 90 | + public int returnone() { |
| 91 | + return 1; |
| 92 | + } |
| 93 | +} |
| 94 | +
|
| 95 | +class DoClass extends EkClass { |
| 96 | + DoClass(int c) { |
| 97 | + super(c); |
| 98 | + System.out.println("I am a constructor"); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +public class cwh_47_this_super { |
| 103 | + public static void main(String[] args) { |
| 104 | + EkClass e = new EkClass(65); |
| 105 | + DoClass d = new DoClass(5); |
| 106 | + System.out.println(e.getA()); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-47/JavaChapter10.pdf)** |
| 112 | + |
| 113 | +**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-47/UltimateJavaCheatSheet.pdf)** |
0 commit comments