DEV Community

S Sarumathi
S Sarumathi

Posted on

Method Overriding

Method overriding is one of the most important concepts in Java Object-Oriented Programming (OOP). It allows a child class to provide its own implementation of a method that is already defined in the parent class.

Example of Method Overriding:

class Animal {

    void sound() {
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {

    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();
        d.sound();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Dog barks
Enter fullscreen mode Exit fullscreen mode

Top comments (0)