Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
As i know that use of overriding that i can use same name function with same parameter declared in bas class in drived class. what is really need of it?

What I have tried:

class a
{
public virtual void add()
{

}
}
class B: A
public override void add()
{

}

what is use of runtime binding. it increased a complexity to understanding else i can do with different name also.

what are advantages and disadvantages of it?
Posted
Updated 13-Mar-17 2:47am
v3
Comments
Kishor-KW 2-Dec-16 2:49am    
yes. now clear to me. i have an same question for virtual function. what is need of it. rather than runtime binding? should i have to post a new question for this?
Philippe Mori 2-Dec-16 11:21am    
Never modify a question in a way that make old response appears off topic. If new question is directly related to the original question, you might append new information and clearly indicate the added section to the question. In any other case, ask a completly new question.

What you are doing is a very bad idea as someone who would have a similar question and use Google might find a question where the answer does not seems to be related to the question.

Even worst, by doing that, you might cause other people answer to be downvoted. Thus it is a complete lack of respect of other people.

This is abusive.

Read these answers, you will get a clear idea.
What is the advantage of Method Overloading in OOPS concept[^]
 
Share this answer
 
v2
Comments
Kishor-KW 2-Dec-16 2:49am    
yes. now clear to me. i have an same question for virtual function. what is need of it. rather than runtime binding? should i have to post a new question for this?
King Fisher 2-Dec-16 2:58am    
edit your question .
Kishor-KW 2-Dec-16 2:59am    
ok
Kishor-KW 2-Dec-16 3:36am    
i edited my question. please answer
King Fisher 2-Dec-16 4:22am    
why did you removed your old question, you should add the second question below your 1st question. its seams we answered irrelevant to your question right now.
Why would having AddInt, AddFloat, AddDouble, AddDecimal, AddByte, AddBigInt have "more readability" than having a single method name that does the same job regardless of the parameter types?

Do you have separate driving tests in your country: DrivingTestFordEscort, DrivingTestFordKa, DrivingTestMercedesAClass, DrivingTestMercedesBClass, ...?
Or do you have a single driving test which allows you to drive a whole class of similar vehicles?

Overloading lets you concentrate on what is happening without focusing on the details, and improves readability and maintainability: if you write your code like this:
C#
for (int i = 0; i < 10; i++)
   {
   for (int j = 0, j < 10; j++)
      {
      Console.WriteLine(Add(i, j));
      }
   }
And then decide you need doubles, it's simple:
C#
for (double i = 0; i < 10; i++)
   {
   for (double j = 0, j < 10; j++)
      {
      Console.WriteLine(Add(i, j));
      }
   }
Because overloading lets you use the "same functionality" without having to find all the instances of AddInt and replace them with AddDouble - the system sorts out which overload is appropriate. This is a trivial example, but if you miss one replacement in a complex piece of code, you introduce a bug, because the system will do some casts for you and you get the "wrong" result: integer division is very different to floating point!

Plus, your example is trivial as well: it's OK when you only want two integers, or twoDoubles, but here is one of my overloaded methods:
C#
public Video Create(string title)
public Video Create(string title, bool temporary)
public Video Create(string title, DateTime productionDate)
public Video Create(Guid id, string title, DateTime productionDate, DateTime         
public Video Create(Guid id, string title, DateTime productionDate, DateTime insertDate, bool complete, bool offline = false, bool deleted = false)
public Video Create(Guid id, string title, DateTime productionDate, DateTime insertDate, bool complete, int playCount, bool offline = false, bool deleted = false)

What names are you going to give those, and how are you going to remember them?
 
Share this answer
 
Quote:
it's overloaded one but what is use of it. in the way of re-usability, readability ? what exactly it give me advantage?
An example of the alternative approach: GLAPI/glVertexAttrib - OpenGL.org[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900