AI Hints
Home›Java Tutorial›Overriding, Polymorphism and Abstract Classes
Java · Lesson 76 of 120

Overriding

Replace a parent method.

⏱ 2 min read▶ Video at 1:35:37🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

A child class can replace a method it inherited, with its own version. That's called overriding. Every animal speaks, but each one sounds different.

  • Replace an inherited method
  • Same name and parameters
  • Called overriding

Animal.java

Animal has a sound method, that returns three dots. Speak uses sound, to build its sentence. Speak doesn't know which animal it is. It just calls sound.

Main.javajava
public class Animal {
    protected String name;
    public Animal(String name) { this.name = name; }

    public String sound() { return "..."; }

    public void speak() {
        System.out.println(name + " says " + sound());
    }
}

Dog.java

Dog overrides sound, with the same name, and the same parameters. Now it returns woof. At override asks Java to check that we really are replacing a parent method.

Main.javajava
public class Dog extends Animal {
    public Dog(String name) { super(name); }

    @Override
    public String sound() { return "Woof"; }
}

Each animal its own sound

Cat overrides sound too, with meow. We call speak on a dog, a cat, and a plain animal. Run it. Speak is the same method for all three. But each one uses its own sound.

Main.javajava
public class Main {
    public static void main(String[] args) {
        new Dog("Rex").speak();
        new Cat("Tom").speak();
        new Animal("Mystery").speak();
    }
}
Output
Rex says Woof
Tom says Meow
Mystery says ...
Also uses Animal.java
public class Animal {
    protected String name;
    public Animal(String name) { this.name = name; }

    public String sound() { return "..."; }

    public void speak() {
        System.out.println(name + " says " + sound());
    }
}
Also uses Dog.java
public class Dog extends Animal {
    public Dog(String name) { super(name); }

    @Override
    public String sound() { return "Woof"; }
}
Also uses Cat.java
public class Cat extends Animal {
    public Cat(String name) { super(name); }

    @Override
    public String sound() { return "Meow"; }
}

Why @Override helps

Here, we typed Sound with a capital S, by mistake. Without at override, Java would quietly make a new method, and the dog would say three dots. Run it. At override catches the typo straight away.

Dog.javajava
public class Dog extends Animal {
    public Dog(String name) { super(name); }

    @Override
    public String Sound() { return "Woof"; }
}
Output
Dog.java:4: error: method does not override or implement a method from a supertype
    @Override
    ^
1 error
error: compilation failed
Also uses Animal.java
public class Animal {
    protected String name;
    public Animal(String name) { this.name = name; }

    public String sound() { return "..."; }

    public void speak() {
        System.out.println(name + " says " + sound());
    }
}
Also uses Main.java
public class Main {
    public static void main(String[] args) {
        new Dog("Rex").speak();
        new Cat("Tom").speak();
        new Animal("Mystery").speak();
    }
}
Also uses Cat.java
public class Cat extends Animal {
    public Cat(String name) { super(name); }

    @Override
    public String sound() { return "Meow"; }
}

Summary

Overriding replaces an inherited method, with the same name and parameters. Always add at override, so Java checks it. Next, the magic this gives us, polymorphism.

Key takeaways
  • Same signature
  • @Override
  • Child's version wins
Next up: Polymorphism — many forms, one call