Table of contents
1.
Introduction
2.
Overloading Methods in Inheritance in C++
3.
Overloading Methods in Inheritance in Java
4.
Overloading Methods in Inheritance in C#
5.
Frequently Asked Questions
5.1.
What is inheritance?
5.2.
How are the functions differentiated from one another in function overloading?
5.3.
Can we implement overloading with inheritance in C++?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Overloading With Inheritance

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Inheritance is the ability to inherit the features and functionalities from another class. It supports the reuse of the same code. Inheritance is the most important feature of object-oriented programming. A class that inherits the member functions and functionality from another class is called the derived class, and the class from which the derived class acquires is called the parent class. At the same time, overloading is the ability to use a single identifier to define more than one class method that differs from one another in terms of their input/output parameters. In case of overloading, the method chosen will be selected at the compile time. This blog will discuss whether overloading works with inheritance in different languages?

Click on the following link to read further: Features of C Language

Overloading Methods in Inheritance in C++

Method overloading is a feature that allows a class to have multiple methods having the same name but different parameters.

Let us take an example to examine whether overloading works with inheritance in C++?


Example

#include<iostream>
using namespace std;

// Base class
class Parent {
    public:
     int increment(int i) 
     { 
      cout << "-----Inside Integer Increment function-----"<<endl; 
      cout<<"Before Value: "<<i<<" After Value: ";
      return i+1; 
     }
};

// Derived class
class Child : public Parent {
    public:
     double increment(double d) 
     { 
      cout << "-----Inside Double Increment function-----"<<endl; 
      cout<<"Before Value: "<<d<<" After Value: ";
      return d+1.3; 
     }
};

int main()
{
Child* obj = new Child;
cout << obj->increment(10) << '\n';
cout << obj->increment(10.5) << '\n';
}


Expected Output

-----Inside Integer Increment function-----
Before Value: 10 After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8


Actual Output

-----Inside Double Increment function-----
Before Value: 10 After Value: 11.3
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8


You can also practice with the help of Online C++ Compiler


Explanation

There is a difference between the expected output and the actual output because there is no overload resolution between the Base class (Parent class) and the Derived class (Child class). When we call the increment() function from the main, the compiler starts to look for it in the scope of the derived class, when it finds the single function “double increment(double)”, the compiler simply calls it. The compiler never bothers with the scope of the Base (Parent class). This happens because, in C++, there is no overloading across the scopes.


Example

#include<iostream>
using namespace std;

// Base class
class Parent {
    public:
     int increment(int i) 
     { 
      cout << "-----Inside Integer Increment function-----"<<endl; 
      cout<<"Before Value: "<<i<<" After Value: ";
      return i+1; 
     }
};

// Derived class
class Child : public Parent {
    public:
    using Parent :: increment;
     double increment(double d) 
     { 
      cout << "-----Inside Double Increment function-----"<<endl; 
      cout<<"Before Value: "<<d<<" After Value: ";
      return d+1.3; 
     }
};

int main()
{
Child* obj = new Child;
cout << obj->increment(10) << '\n';
cout << obj->increment(10.5) << '\n';
}


Expected Output

-----Inside Integer Increment function-----
Before Value: 10 After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8


Actual Output

-----Inside Integer Increment function-----
Before Value: 10 After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5 After Value: 11.8


Explanation 

There is no difference between the actual and the expected output for the above code as we have created an overload set of all increment() functions from the base and derived class with the help of the “using” declaration.

Overloading Methods in Inheritance in Java

Let us take an example to examine whether overloading works with inheritance in Java?


Example

// Base class
class Parent
{
    public int increment(int i)
    {
        System.out.println("-----Inside Integer Increment function-----");
        System.out.println("Before Value: " + i);
        return i+1;
    }
}
// Derived class
class Child extends Parent
{
    public double increment(double i)
    {
        System.out.println("-----Inside Double Increment function-----");
        System.out.println("Before Value: " + i);
        return i + 1.3;
    }
}
class overloadingInJava
{
    public static void main(String args[])
    {
        Child obj = new Child();
        System.out.println(" After Value: " + obj.increment(10));
        System.out.println(" After Value: " + obj.increment(10.5));
    }
}


Expected Output

-----Inside Integer Increment function-----
Before Value: 10
After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5
After Value: 11.8


Actual Output

-----Inside Integer Increment function-----
Before Value: 10
After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5
After Value: 11.8


Explanation

There is no difference between the expected output and actual output for the above code because, in Java, overloading works across scopes, i.e., the Java compiler determines the correct form of the overload method to be executed at compile time based on the type of argument used to call the method.

Overloading Methods in Inheritance in C#

Let us take an example to examine whether overloading works with inheritance in C#?


Example

using System;   
// Base class
class Parent
{
    public int increment(int i)
    {
        Console.WriteLine("-----Inside Integer Increment function-----");
        Console.WriteLine("Before Value: " + i);
        return i + 1;
    }
}
// Derived class
class Child : Parent
{
    public double increment(double i)
    {
        Console.WriteLine("-----Inside Double Increment function-----");
        Console.WriteLine("Before Value: " + i);
        return i+1.3;
    }
}
class MyProgram
{  
    static void Main(string[] args)
    {
        Child obj = new Child();
        Console.WriteLine("After Value: "+obj.increment(10));
        Console.WriteLine("After Value: "+obj.increment(10.5));
        Console.ReadKey(); // this is used to halt the console
    }
}


Expected Output

-----Inside Integer Increment function-----
Before Value: 10
After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5
After Value: 11.8


Actual Output

-----Inside Double Increment function-----
Before Value: 10
After Value: 11.3
-----Inside Double Increment function-----
Before Value: 10.5
After Value: 11.8

 
Explanation

There is a difference between the expected output and the actual output for the above code as we have created an object of the child (derived) class, as a result of which the compiler will give more preference to the methods of the child class and if needed will perform implicit typecasting. For the above example, when the compiler encounters “ Console.WriteLine("After Value: "+obj.increment(10))“ will check for the parameter compatibility, in this case, 10, which is an int value, is compatible with the double of child class function increment; therefore, the compiler will perform the implicit type conversion of int to double and will output a value 11.3. 

Similarly, when the compiler encounters “ Console.WriteLine("After Value: "+obj.increment(10.5))” will check for the parameter compatibility again, in this case, 10.5 is a double value that is compatible with the double of child class function increment, and therefore it will output 11.8.


In case we swap the functions, i.e., if the function of type int is defined in the child class and the function of type double is defined in the parent class, then :


Example

using System;   
// Base class
class Parent
{
    public double increment(double i)
    {
        Console.WriteLine("-----Inside Double Increment function-----");
        Console.WriteLine("Before Value: " + i);
        return i+1.3;
    }
}
// Derived class
class Child : Parent
{
    public int increment(int i)
    {
        Console.WriteLine("-----Inside Integer Increment function-----");
        Console.WriteLine("Before Value: " + i);
        return i + 1;
    }
}
class MyProgram
{  
    static void Main(string[] args)
    {
        Child obj = new Child();
        Console.WriteLine("After Value: "+obj.increment(10));
        Console.WriteLine("After Value: "+obj.increment(10.5));
        Console.ReadKey(); // this is used to halt the console
    }
}


Expected Output

-----Inside Integer Increment function-----
Before Value: 10
After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5
After Value: 11.8


Actual Output

-----Inside Integer Increment function-----
Before Value: 10
After Value: 11
-----Inside Double Increment function-----
Before Value: 10.5
After Value: 11.8


Explanation

There is no difference between the expected output and the actual output for the above code because we have created an object of the Child class (Derived), as a result of which more preference is given to the methods of the Child class. When the compiler comes, it encounters the statement “Console.WriteLine("After Value: "+obj.increment(10))” will check for the parameter compatibility, in this case, 10, which is an int value that is compatible with the int of child class function increment, therefore it will output a value 11. 

When the compiler encounters “Console.WriteLine("After Value: "+obj.increment(10.5))” will check for the parameter compatibility, in this case, 10.5, which is a double value that is not compatible with the int of child class function increment, therefore, now, the compiler will prefer the methods of the parent (base) class, and if a match is found it will execute that one. Because of this, the compiler will output 10.8 using the double increment(), a method function of the parent class.

This happens because, just like C++, there is no overload resolution between the Child class and Parent class in C# too. In C#, just like C++, there is no overloading across the scope.

Related Article: Features of Object Oriented Programming

Know about Single Inheritance in Java in detail.

Frequently Asked Questions

What is inheritance?

Inheritance is defined as the ability of a class to inherit the features and functionalities from another class.
 

How are the functions differentiated from one another in function overloading?

In case of overloading, the functions are differentiated from one another as they have different types and numbers of parameters as their arguments.
 

Can we implement overloading with inheritance in C++?

Yes, we can implement overloading with inheritance in C++ with the help of the “using” keyword.

Conclusion

In this article, we have extensively discussed the Overloading with inheritance in different programming languages.

After reading about the Overloading with inheritance, are you not feeling excited to read/explore more articles on Inheritance? Don't worry; Coding Ninjas has you covered. To learn, see what is multi-level inheritanceMultiple Inheritance in Java, and Hybrid Inheritance in Java.

If you wish to enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, and many more, then you should check out our Guided path column at Coding Ninjas Studio. We at Coding Ninjas Studio organize many contests in which you can participate. You can also prepare for the contests and test your coding skills by giving the mock test series available. In case you have just started the learning process, and your dream is to crack major tech giants like Amazon, Microsoft, etc., then you should check out the most frequently asked problems and the interview experiences of your seniors that will surely help you in landing a job in your dream company. 

Do upvote if you find the blogs helpful.

Happy Learning!

Live masterclass