Skip to content

Commit bf90f65

Browse files
authored
Update 2022-11-03-method-overriding-in-java.md
1 parent 3d93a0a commit bf90f65

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

‎_posts/java-interview-questions/core-java-interview-questions/2022-11-03-method-overriding-in-java.md

+24-27
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,48 @@ In this tutorial, we will see the method overriding in Java.
1111

1212
When **a child class provides a specific implementation for the method already declared in parent class**, it is called a method overriding.
1313

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.
1515

1616
Example:-
1717

1818
```java
1919
/**
20-
* A Java Program to explain method oeverriding.
20+
* A Java Program to explain the method of overriding.
2121
* @author coderolls.com
2222
*
2323
*/
2424
public class Test {
25-
public static void main(String[] args) {
26-
Dog dog = new Dog();
27-
Cat cat = new Cat();
28-
29-
dog.printSound();
30-
cat.printSound();
31-
}
25+
public static void main(String[] args) {
26+
Dog dog = new Dog();
27+
Cat cat = new Cat();
28+
29+
dog.printSound();
30+
cat.printSound();
31+
}
3232

3333
}
3434
class Animal {
35-
36-
public void printSound() {
37-
System.out.println("Print sound of animal");
38-
}
35+
public void printSound() {
36+
System.out.println("Print sound of animal");
37+
}
3938
}
4039

4140
class Dog extends Animal {
42-
43-
@Override
44-
public void printSound() {
45-
System.out.println("Dogs bark..!");
46-
}
41+
@Override
42+
public void printSound() {
43+
System.out.println("Dogs bark..!");
44+
}
4745
}
4846

4947
class Cat extends Animal {
50-
51-
@Override
52-
public void printSound() {
53-
System.out.println("Cats meow..!");
54-
}
48+
@Override
49+
public void printSound() {
50+
System.out.println("Cats meow..!");
51+
}
5552
}
5653
```
5754

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

Comments
 (0)