You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/java-interview-questions/core-java-interview-questions/2022-11-03-method-overriding-in-java.md
+24-27
Original file line number
Diff line number
Diff line change
@@ -11,51 +11,48 @@ In this tutorial, we will see the method overriding in Java.
11
11
12
12
When **a child class provides a specific implementation for the method already declared in parent class**, it is called a method overriding.
13
13
14
-
So both parent class and child class will have the same method but with different implementation.
14
+
So both parent class and child class will have the same method but with different implementations.
15
15
16
16
Example:-
17
17
18
18
```java
19
19
/**
20
-
* A Java Program to explain method oeverriding.
20
+
* A Java Program to explain the method of overriding.
21
21
* @author coderolls.com
22
22
*
23
23
*/
24
24
publicclassTest {
25
-
publicstaticvoidmain(String[] args) {
26
-
Dog dog =newDog();
27
-
Cat cat =newCat();
28
-
29
-
dog.printSound();
30
-
cat.printSound();
31
-
}
25
+
publicstaticvoidmain(String[] args) {
26
+
Dog dog =newDog();
27
+
Cat cat =newCat();
28
+
29
+
dog.printSound();
30
+
cat.printSound();
31
+
}
32
32
33
33
}
34
34
classAnimal {
35
-
36
-
publicvoidprintSound() {
37
-
System.out.println("Print sound of animal");
38
-
}
35
+
publicvoidprintSound() {
36
+
System.out.println("Print sound of animal");
37
+
}
39
38
}
40
39
41
40
classDogextendsAnimal {
42
-
43
-
@Override
44
-
publicvoidprintSound() {
45
-
System.out.println("Dogs bark..!");
46
-
}
41
+
@Override
42
+
publicvoidprintSound() {
43
+
System.out.println("Dogs bark..!");
44
+
}
47
45
}
48
46
49
47
classCatextendsAnimal {
50
-
51
-
@Override
52
-
publicvoidprintSound() {
53
-
System.out.println("Cats meow..!");
54
-
}
48
+
@Override
49
+
publicvoidprintSound() {
50
+
System.out.println("Cats meow..!");
51
+
}
55
52
}
56
53
```
57
54
58
-
1. In above example `Animal` is the parent class. It has `printSound()` method with it's own implementation.
59
-
2. Next, we have created a child class `Dog` by extending the parent class `Animal`. We know dogs do bark so we can provide specific implementation for the `printSound()` method in `Dog` class.
60
-
3. Also, we have created a child class `Cat` by extending the parent class `Animal`. We can again provide cat specific implementation for the `printSound()` method in `Cat` class.
61
-
4. In Test class, we have created objects of Dog and Cat class and invoked their `printSound()` method. We can see, when we invoke the `printSound()` method on Dog obejct `d`og, it is printing the `Dogs bark..!` i.e. dog specific implementation. And when we invoke the `printSound()` method on Cat obejct`cat`, it is printing the `Cats meow..!` i.e. dog specific implementation
55
+
1. In the above example `Animal` is the parent class. It has the `printSound()` method with its own implementation.
56
+
2. Next, we have created a child class `Dog` by extending the parent class `Animal`. We know dogs do bark so we can provide the specific implementation for the `printSound()` method in the`Dog` class.
57
+
3. Also, we have created a child class `Cat` by extending the parent class `Animal`. We can again provide the cat-specific implementation for the `printSound()` method in the`Cat` class.
58
+
4. In the Test class, we have created objects of the Dog and Cat class and invoked their `printSound()` method. We can see, that when we invoke the `printSound()` method on Dog object `dog`, it prints the `Dogs bark..!` i.e. dog specific implementation. And when we invoke the `printSound()` method on the Cat object`cat`, it prints the `Cats meow..!` i.e. dog specific implementation
0 commit comments