CodeGym/Java Blog/Methods in Java/Method Overriding in Java
Author
Aditi Nawghare
Software Engineer at Siemens

Method Overriding in Java

Published in the Methods in Java group
members
Hi! You're already using Java methods and know a lot about them. You've probably faced the situation where one class has many methods with the same name but different parameters. You'll recall that in those cases we used method overloading. Today we're considering another situation. Imagine that we have a single shared method, but it must do different things in different classes. How do we implement this behavior? To understand, let's consider an Animal parent class, which represents animals, and we'll create a speak method in it:
public class Animal {

   public void speak() {

       System.out.println("Hello!");
   }
}
Though we've only just started writing the program, you probably see a potential problem: there are a lot of animals in the world, and they all 'speak' differently: cats meow, ducks quack, and snakes hiss. How method overriding works - 2Our goal is simple: avoid creating loads of speaking methods. Instead of creating a catSpeak() method for meowing, a snakeSpeak() method for hissing, etc., we want to call the speak() method and have the snake hiss, the cat meow, and the dog bark. We can easily achieve this using method overriding. Wikipedia gives the following explanation of the term 'overriding': Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes That is essentially correct. Method overriding lets you take some method of the parent class and write your own implementation in each child class. The new implementation 'replaces' the parent's implementation in the child class. Let's see how this looks in an example. Create 4 classes that inherit our Animal class:
public class Bear extends Animal {
   @Override
   public void speak() {
       System.out.println("Growl!");
   }
}
public class Cat extends Animal {

   @Override
   public void speak() {
       System.out.println("Meow!");
   }
}

public class Dog extends Animal {

   @Override
   public void speak() {
       System.out.println("Woof!");
   }
}


public class Snake extends Animal {

   @Override
   public void speak() {
       System.out.println("Hiss!");
   }
}
"Here's a small lifehack for the future: to override the parent class's methods, go to the child class's code in IntelliJ IDE, Click Ctrl+O, and choose "Override methods..." in the menu. Get used to using hot keys from the outset — it will help you write programs faster! To specify the behavior we need, we did a few things:
  1. In each child class, we created a method with the same name as the parent class's method.

  2. We told the compiler that naming the method the same as in the parent class wasn't happenstance: we want to override its behavior. To communicate this to the compiler, we set the @Override annotation above the method.
    When placed above a method, the @Override annotation informs the compiler (as well as programmers reading your code): 'Everything is okay. This isn't a mistake. I'm not being forgetful. I'm aware that such a method already exists and I want to override it'.

  3. We wrote the implementation we need for each child class. When the speak() method is called, a snake should hiss, a bear should growl, etc.
Let's see how this works in a program:
public class Main {

   public static void main(String[] args) {

       Animal animal1 = new Dog();
       Animal animal2 = new Cat();
       Animal animal3 = new Bear();
       Animal animal4 = new Snake();

       animal1.speak();
       animal2.speak();
       animal3.speak();
       animal4.speak();
   }
}
Console output:
Woof!
Meow!
Growl!
Hiss!
Excellent! Everything works as it should! We created 4 reference variables that store objects of the Animal parent class, and we assigned instances of 4 different child classes to them. As a result, each object exhibits its own behavior. For each child class, the overridden speak() method replaced the 'native' speak() method in the Animal class (which simply displays 'Hello!'). How method overriding works - 3Overriding has several limitations:
  1. The overridden method must have the same parameters as the parent method.

    If the parent class's speak method has a String parameter, the overridden method in the child class must also have a String parameter. Otherwise, the compiler will generate an error:

    public class Animal {
    
       public void speak(String s) {
    
           System.out.println("Hello! " + s);
       }
    }
    
    public class Cat extends Animal {
    
       @Override // Error!
       public void speak() {
           System.out.println("Meow!");
       }
    }

  2. The overridden method must have the same return type as the parent method.

    Otherwise, we'll get a compiler error:

    public class Animal {
    
       public void speak() {
    
           System.out.println("Hello!");
       }
    }
    
    
    public class Cat extends Animal {
    
       @Override
       public String speak() {         // Error!
           System.out.println("Meow!");
           return "Meow!";
       }
    }

  3. The access modifier on the overridden method must also be the same as the 'original' method:

    public class Animal {
    
          public void speak() {
    
                System.out.println("Hello!");
          }
    }
    
    public class Cat extends Animal {
    
           @Override
           private void speak() {      // Error!
               System.out.println("Meow!");
           }
    }
Method overriding in Java is one way to implement polymorphism (the principle of OOP that we described in the last lesson). This means that its main advantage is the same flexibility that we discussed previously. We can build a simple and logical system of classes, each having a specific behavior (dogs bark, cats meow), with a common interface — a single speak() method for them all rather than loads of methods, e.g. dogSpeak(), speakCat(), etc.
Comments (16)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
z18335776829
Level 19 , China, China
7 May 2023, 14:29
除了方法体外其余都相同
Mateusz
Level 15 , Poland, Poland
24 July 2022, 19:41
Another article short and informative. Do you like more:)
啦啦 QA Automation Engineer at Intuit
2 December 2021, 11:32
个人感觉应该是这样描述: 1.被覆盖的方法必须与父方法具有相同的参数。 2.被覆盖的方法必须与父方法具有相同的返回类型,或者是该类型的子类 3,覆盖方法上的访问修饰符也必须与“原始”方法相同,或者大于父类修饰符(父类f方法protected--子类方法public)
Chandan Thapa
Level 22 , Dubai, United Arab Emirates
16 November 2020, 08:29
This is good!
Sela
Level 20 , Poland
15 July 2020, 09:24
return types - if they are not the same - have to be at least covariant. covariant means that in child class return type has to be subtype of parent return type
Chang You
Level 47 , Santa Rosa, United States
24 December 2020, 19:13
What does "subtype" mean? Could you give an example?
Peter Schrijver
Level 23 , Hilversum, Netherlands
22 June 2020, 05:14
Nice and concise
Tomasz Sknadaj
Level 18 , Skoczów, Polska
1 June 2020, 19:58
Great article, but i have one question - is something wrong with my IntelliJ ? I write code about animal and cat and i try to use this shortcut "Click Ctrl+O, and choose "Override methods..." in the menu" And i got this:
public static class Cat extends Animal{
        @Override
        public void speak() {
            super.speak();
        }
    }
So anyway i has to change super.speak to System.out.println("meow"). Is it normal or i have to change something in my IntelliJ?
orangepeeler
Level 14 , Not in list, United States
3 September 2020, 02:22
did it work? you should be good to go.
Onur Bal
Level 27 , Istanbul, Turkey
14 September 2020, 15:41
Nothing is wrong. super.speak(); can be broken down as such: Just as "this" refers to the object you are dealing with, "super" refers to the parent class. And by using super, you can make use of the methods and variables found in the parent class. When you execute super.speak();, it goes to the parent class and finds the speak method and executes it for you. This can be useful if you want to make use of the method in the parent class, but also add something of your own to it in a given subclass
Sansho
Level 19 , Bordeaux, France
8 May 2021, 08:51
Well, now I guess you figured it out, but for the new readers: the "super.method();" generated by IntelliJ is just optional, it only prevents to "underride" the method with an empty code :)
Vesa
Level 41 , Kaliningrad, Russia
11 December 2019, 10:02
"The overridden method must have the same parameters as the parent method" Maybe there should be "The overriding method"?
MaGaby2280
Level 41 , Guatemala City, Guatemala
21 November 2019, 19:21
Excellent explanation!!!
Robert Constantinescu
Level 25 , Bucharest, Romania
3 November 2019, 17:45
nice, good job explaining