C#
  Home arrow C# arrow Overriding versus Overloading
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Overriding versus Overloading
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-06-10

    Table of Contents:
  • Overriding versus Overloading
  • Method Overloading Example
  • Overload a Constructor
  • Overriding

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Overriding versus Overloading


    (Page 1 of 4 )

    Two very important concepts in object-oriented programming are overriding and overloading. Overloading is about creating multiple methods with the same name, but different signatures, in the same scope. Overriding is about changing the behavior of a certain method in the child class from the way it is behaving in the parent class. The rest of this article will explore these two concepts in detail.

    Overloading occurs when a method has more than one definition in the same scope. It's important to remember two key points from the previous statement: same name and same scope. The method implementations have the same name because they do similar tasks. For instance, if we need to implement a method that gets the student name, there are many ways to do that. We can get the name using an id and we can get the name using a social security number. One way to implement the methods is as follows:


      class   Student

    {

    ...

    public   string GetName( int id) {...}

    public   string GetName( string ssn) {...}

    ...

    }


    The method GetName() is overloaded since the two implementations are in the same scope (class scope) and have the same name. If the methods are declared in different scopes (for example, different classes), then we are not talking about overloading.

    The first question that comes to mind is: do we have to declare them with the same name? The answer is no. We could have named them GetName1() and GetName2(), or even GetNameById() and GetNameBySSN(). So overloading is not mandatory; it is just a helpful feature in object-oriented languages (like C++, Java, and C#). It is common sense since both methods are doing pretty much the same thing.

    Because the methods have the same name, the compiler will use the signature to determine what method to call under different scenarios. The compiler will be able to tell the difference using the method signature, which has to do with the method parameters. Be definition, the method signature is the name of the method and its parameters. Since the name in this case is the same, what is left is the parameters. There are three ways to distinguish one set of parameters from the other:

    1. Parameter count - one method could have 2 parameters while the other has 3. This way, the compiler will call the method with the correct number of parameters.

    2. Parameter type - if two methods have the same parameter count, but different types (int, string, long …), then it is easy for the compiler to know which method to call.

    3. Parameter order - when we have a match in the parameter count and parameter type, but with a different order, then it is again easy for the compiler to know which method to call. For example:


    public   string GetName( int id, string match) {...}

    public   string GetName( string match, int id) {...}

    More C# Articles
    More By Ayad Boudiab


       · Welcome to my latest article on overriding versus overloading.I am looking forward...
     

    C# ARTICLES

    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT