|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Overriding |
| 4 | +--- |
| 5 | + |
| 6 | +Reimplementing the inherited method from the parent class in the child class is called __Overriding in Java__. |
| 7 | + |
| 8 | +There are certain __rules for overriding__, in the below code we will point out all of the rules: |
| 9 | + |
| 10 | +{% highlight java linenos %} |
| 11 | + |
| 12 | +class Animal { |
| 13 | + |
| 14 | + private void drink() { |
| 15 | + System.out.println("Animal Drink"); |
| 16 | + } |
| 17 | + |
| 18 | + protected void walk() { |
| 19 | + System.out.println("Animal Walk"); |
| 20 | + } |
| 21 | + |
| 22 | + public void eat() { |
| 23 | + System.out.println("Animal Eat"); |
| 24 | + } |
| 25 | + |
| 26 | + public void run() { |
| 27 | + System.out.println("Animal Run"); |
| 28 | + } |
| 29 | + |
| 30 | + public void sleep() throws IOException { |
| 31 | + System.out.println("Animal Sleep"); |
| 32 | + } |
| 33 | + |
| 34 | + public Animal getAnimal() { |
| 35 | + return new Animal(); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class Horse extends Animal { |
| 40 | + |
| 41 | + private void drink() { // Not a method override as drink() |
| 42 | + // wasn't inherited by Horse class |
| 43 | + System.out.println("Horse Drink"); |
| 44 | + } |
| 45 | + |
| 46 | + private void eat() { // You can't use a more |
| 47 | + // restrictive access modifier |
| 48 | + // (gives you a compiler error) |
| 49 | + System.out.println("Horse Eat"); |
| 50 | + } |
| 51 | + |
| 52 | + public void walk() { // Valid method override as you can use less restrictive |
| 53 | + // access modifier in the overriding method |
| 54 | + System.out.println("Horse Walk"); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public void run(int n) { // Not a method override (argument list differs) |
| 59 | + // but rather a method overload of run() in Animal |
| 60 | + System.out.println("Horse Run"); |
| 61 | + } |
| 62 | + |
| 63 | + public Horse getAnimal() { // Valid method override as return type must |
| 64 | + // be the same as, or a subtype of, the return type |
| 65 | + // declared in the original overridden method in the superclass |
| 66 | + return new Horse(); |
| 67 | + } |
| 68 | + |
| 69 | + public void eat() throws Exception { // Not a method override as the overridden method doesn't |
| 70 | + // throw any checked exceptions while overriding method does |
| 71 | + // (gives a compiler error) |
| 72 | + System.out.println("Horse Eat"); |
| 73 | + } |
| 74 | + |
| 75 | + public void sleep() throws FileSystemException { // Valid method override as FileSystemException |
| 76 | + // is a subclass of IOException |
| 77 | + System.out.println("Horse Sleep"); |
| 78 | + } |
| 79 | + |
| 80 | + public void sleep() throws Exception { // Not a method override as Exception is neither |
| 81 | + // same as nor a subclass of IOException |
| 82 | + // (gives a compiler error) |
| 83 | + System.out.println("Horse Sleep"); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +public class Overriding { |
| 88 | + |
| 89 | + public static void main (String [] args) { |
| 90 | + Animal a = new Animal(); |
| 91 | + Animal b = new Horse(); // Animal ref, but a Horse object |
| 92 | + Horse h = new Horse(); |
| 93 | + |
| 94 | + a.eat(); // Runs the Animal version of eat() |
| 95 | + b.eat(); // Runs the Horse version of eat() |
| 96 | + // (Concept called Dynamic method invocation) |
| 97 | + |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +{% endhighlight %} |
| 102 | + |
| 103 | +Some more rules which may be obvious: |
| 104 | + |
| 105 | +* You cannot override a method marked `final`. |
| 106 | +* You cannot override a method marked `static`. |
| 107 | +* If a method can't be inherited, you cannot override it. As said earlier, |
| 108 | +overriding implies that you're reimplementing a method you inherited. |
0 commit comments