Overriding Methods

Learn how to override methods in Java.

We'll cover the following

What is method overriding?

Method overriding occurs when a public method in a subclass has the same method signature (method name, parameter type list, and return type) as a public method in the superclass. For example:

// parent class
public class A
{
    public void func()
    {
       // do something
    }  
}

// child class
public class B extends A
{
    public void func()
    {
       // do something
    }  
}

Notice that both classes A and B have the same func method in them. This is method overriding.

In such a case, the method in the subclass will be called instead of the method in the superclass. See the executable below.

Get hands-on with 1400+ tech skills courses.