Overloading | What Is The Difference Between Overriding And Overloading In Java?

Here in this tutorial, we will learn What Is The Difference Between Overriding And Overloading In Java? In polymorphism, we can see the use of the single word in two or more ways that is the same in overloading and overriding where we use the same word in different ways by making the situation.

Overriding and overloading

What Is Overriding?

overriding when we have an inheritance situation and the child class has the same function signature as of parent class and to same return types in terms of the number of arguments. We can understand by using an example so let’s see an example.

class Parent {
    void foo(double d) {
        // do something
    }
}

class Child extends Parent {

    @Override
    void foo(double d){
        // this method is overridden.  
    }
}

As shown in code foo is the function that is there in parents and child in both classes and that too with the same return types and parameters That is what we call overring and to call the such function the object has to decide.

This means the object of the parent class can only call a function in the parent class whereas the function in the child class can be called only by the object of the child.

Let’s learn about overloading

What Is Overloading?

Overloading a function is a way to use the same name of the function in a number of ways just by changing either the number of arguments or the data type of the function. Which makes the overloading of the function.

Let’s take an example to understand more

void foo(int a)
void foo(int a, float b)

Here the name of both the functions is the same but the number of arguments is different which will be differentiated while calling projects where if the function call has given a single argument then the first function is called and if two arguments are given then the second argument is called.

Even if the number of arguments is the same but their data types are different then also we can overload the functions.

Learn more about overloading and overriding at: Overloading vs Overriding

and also visit JAVA TUTORIAL for learning more about java and solving problems related to java.

Leave a Comment

%d bloggers like this: