Hi all,
I know that one advantage of method overloading is that the implementation can be overridden for the same method name, but wanted to know how is that going to be an advantage for us.
All the links explain how to do it, and not why to do it and how itself can be an advantage, could someone please explain.

Thanks in advance. :)

Recommended Answers

All 5 Replies

It can be an advantage if designers want to customize functionallity in the child class. Say a class has a particular behaviour for "Cars" in general, but if you want to call a certain function when the car is say for instance a convertible or something else then polymorphism helps.

I'm not sure of your understanding of polymorphism. Polymorphism is not method overloading.

Method overloading is a feature which allows the creation of methods with the same name which differ from each other in the type of the input and the output of the function. Each of these methods will be present in the same class. The resaon for it is to allow a method to perform many different tasks.

Polymorphism involves the creation of a base class, which contains functionality you want all of your child classes to have. But you want to do something slightly different for each child's implementation of this functionality.

This is incredibly useful for OO programming. Take, for instance, the example of a Character class:

You have a base class Character

class Character
{

    public Character(){}

    public virtual void move(){}

}

You also have this virtual function called move(). The virtual keyword allows this method to be overridden in any derived classes.

Using this info, we can straight away see the benefit of this. Consider in game development, we have a HumanPlayer character and an AI(computer controlled) character. They are both characters, so they can inherit from our base Character class. They also should both move, but we don't want them to move in the same way!

We want our human character to be controlled by input from the user, and we want the AI characters movement to be controlled by our AI system. This is where polymorphism comes in handy.

We derive our classes from our base class:

class Human : Character
{
    public Human(){}

    public override void move()
    {
        if(keyboard.isPressed(W))
        {
            position += 1;
        }
    }
}

We then use an override keyword to override the virtual method from our base class. Then, again with our AI character:

class AICharacter  : Character
{
    public AICharacter(){}

    public override void move()
    {
        position.x += 1;
    }
}

Now, if you don't see the benefit yet, then hang on!

In our game loop, we want to loop through every object in the world. In this case, we only have characters, so we loop through every character in our world. We want to do this because we want each of our characters to move in the way we decided. Therefore, if we have some sort of list of all of our characters, we loop through it and call the move function:

Character[] charactersArray = new Character[3];

for(int i=0; i < charactersArray.length;i++)
{
    charactersArray[i].move();
}

As you can see, we create a Generic list of Character objects, which can contain any type of character. Because of the genius of polymorphism, we can then call the virtual function move() of Character, and it will in turn call the overridden function for the current Character!

So, if there is a Human Character at the charactersArray[0]position, the loop will call Humans version of move() etc..

Without polymorphism, we would have to separate our 3 different types of characters into their own arrays, and loop through each one calling the move() method.
Hopefully that made sense!

Check out this simple example, here you can see what polymoprhism is all about:

    class Program
    {
        static void Main(string[] args)
        {
            Car c = new BMW();
            c.Name();
            c = new Honda();
            c.Name();
            c = new Civic();
            c.Name();
            c = new CBR();
            c.Name();
            //1. sealed - when using a reference of base class:
            c = new CBR1000();
            c.Name();
            //2. sealed when using a reference of particular derived class:
            CBR1000 cbr1000 = new CBR1000();
            cbr1000.Name();
            Console.ReadLine();
        }
    }

    abstract class Car
    {
        public abstract void Name();
    }

    class BMW : Car
    {
        public override void Name()
        {
            Console.WriteLine("This is a BMW car.");
        }
    }

    class Honda : Car
    {
        public override void Name()
        {
            Console.WriteLine("This is a honda car. ");
        }
    }

    class Civic : Honda
    {
        public override void Name()
        {
            base.Name();
            Console.WriteLine("Model: Civic.");
        }
    }

    class CBR : Honda
    {
        public sealed override void Name()
        {
            Console.WriteLine("This is not a car. Its a Honda CBR motorbike. ");
        }
    }

    class CBR1000 : CBR
    {
        public new void Name()
        {
            base.Name();
            Console.WriteLine("Exactly a CBR 1000 model.");
        }
    }

You seem to have 'Method Overloading' confused with the term 'Method Overriding'. Overloading is using the same method within a class with different return types or argument types. Method overriding is used with polymorphism to allow different functionality to be achieved in derived classes than in the parent class.

Could be.
Override mean you set some method as virtual,(in 1st class), then in next class you override it by using override keyword (in a method signiture).
While overload that you have in one (same) class more method with the same name, but with different signitures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.