|
| 1 | +# Method Overriding in Java |
| 2 | + |
| 3 | +### Method Overriding in Java: |
| 4 | +- If the child class implements the same method present in the parent class again, it is know as method overriding. |
| 5 | +- Method overriding helps us to classify a behavior that is specific to the child class. |
| 6 | +- The subclass can override the method of the parent class only when the method is not declared as final. |
| 7 | +- Example : |
| 8 | +- In the below code, we've created two classes: class A & class B. |
| 9 | +- Class B is inheriting class A. |
| 10 | +- In the main() method, we've created one object for both classes. We're running the meth1() method on class A and B objects separately, but the output is the same because the meth1() is defined in the parent class, i.e., class A. |
| 11 | + |
| 12 | +``` |
| 13 | +class A{ |
| 14 | + public void meth1() { |
| 15 | + System.out.println("I am method 1 of class A"); |
| 16 | + } |
| 17 | +} |
| 18 | +
|
| 19 | +class B extends A { |
| 20 | +
|
| 21 | +} |
| 22 | +public class CWH { |
| 23 | + public static void main(String[] args) { |
| 24 | + A a = new A(); |
| 25 | + a.meth1(); |
| 26 | +
|
| 27 | + B b = new B(); |
| 28 | + b.meth1(); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +``` |
| 34 | +Output : |
| 35 | +I am method 1 of class A |
| 36 | +I am method 1 of class A |
| 37 | +``` |
| 38 | + |
| 39 | +- Now, let's see how we can override the meth1() for class B : |
| 40 | + |
| 41 | +``` |
| 42 | +class A{ |
| 43 | + public void meth1() { |
| 44 | + System.out.println("I am method 1 of class A"); |
| 45 | + } |
| 46 | +} |
| 47 | +
|
| 48 | +class B extends A { |
| 49 | + @Override |
| 50 | + public void meth1(){ |
| 51 | + System.out.println("I am method 1 of class B"); |
| 52 | + } |
| 53 | +} |
| 54 | +public class CWH1 { |
| 55 | + public static void main(String[] args) { |
| 56 | + A a = new A(); |
| 57 | + a.meth1(); |
| 58 | +
|
| 59 | + B b = new B(); |
| 60 | + b.meth1(); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +``` |
| 66 | +Output : |
| 67 | +I am method 1 of class A |
| 68 | +I am method 1 of class B |
| 69 | +``` |
| 70 | + |
| 71 | +### Code as described in the video: |
| 72 | + |
| 73 | +``` |
| 74 | +class A { |
| 75 | + public int a; |
| 76 | + public int harry() { |
| 77 | + return 4; |
| 78 | + } |
| 79 | + public void meth2() { |
| 80 | + System.out.println("I am method 2 of class A"); |
| 81 | + } |
| 82 | +} |
| 83 | +
|
| 84 | +class B extends A { |
| 85 | + @Override |
| 86 | + public void meth2() { |
| 87 | + System.out.println("I am method 2 of class B"); |
| 88 | + } |
| 89 | + public void meth3() { |
| 90 | + System.out.println("I am method 3 of class B"); |
| 91 | + } |
| 92 | +} |
| 93 | +
|
| 94 | +public class cwh_48_method_overriding { |
| 95 | + public static void main(String[] args) { |
| 96 | + A a = new A(); |
| 97 | + a.meth2(); |
| 98 | +
|
| 99 | + B b = new B(); |
| 100 | + b.meth2(); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +``` |
| 106 | +Output: |
| 107 | +I am method 2 of class A |
| 108 | +I am method 2 of class B |
| 109 | +``` |
| 110 | + |
| 111 | +**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-48/JavaChapter10.pdf)** |
| 112 | + |
| 113 | +**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-48/UltimateJavaCheatSheet.pdf)** |
0 commit comments