C#
  Home arrow C# arrow Page 4 - 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 
Mobile Linux 
App Generation ROI 
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 - Overriding


    (Page 4 of 4 )

    As mentioned earlier, overriding has to do with parent and child classes. If you are not satisfied with the implementation of a method in the parent class, you can keep the same declaration (signature and return type), but provide a different implementation. For example, a Rectangle class inherits the drawing functionality from a Shape class, but it overrides this functionality in order to be able to draw a rectangle.

    We have been using overriding all along in our examples. In C#, everything is an object, so when we create a class ( Employee , Fraction …) that does not specifically inherit from another class, it automatically inherits the functionality of an Object. Notice that in our previous examples, we were able to use the ToString() method to print an object as a string. The ToString() method is defined in the Object class. Since the Object class is too general, this method cannot give us anything useful, so we override it.

    Ponder the following example:


    namespace OverridingExample

    {

    class   Program

    {

    static   void Main( string [] args)

    {

    Employee e = new   Employee ( "Jim" , "Tester" , 38, 15.95);

    Console .WriteLine(e);

    Manager m = new   Manager ( "Jennifer" , "Jones" , 40, 25, 350);

    Console .WriteLine(m);

    }

    }


    class   Employee

    {

    protected   string firstName;

    protected   string lastName;

    protected   int hoursWorked;

    protected   double ratePerHour;


    public Employee( string f_name, string l_name,

    int hours, double rate)

    {

    firstName = f_name;

    lastName = l_name;

    hoursWorked = hours;

    ratePerHour = rate;

    }


    public   virtual   double CalculatePay()

    {

    return hoursWorked * ratePerHour;

    }


    public   override   string ToString()

    {

    return   string .Format( "Name: {0} {1}nPay: {2}" ,

    firstName, lastName, CalculatePay());

    }

    }


    class   Manager : Employee   //Manager inherits from Employee

    {

    private   double bonus;


    public Manager( string f_name, string l_name,

    int hours, double rate, double bonus)

    : base (f_name, l_name, hours, rate)

    {

    this .bonus = bonus;

    }


    public   override   double CalculatePay()

    {

    return   base .CalculatePay() + bonus;

    }

    }

    }


    In the Employee class, we are overriding the ToString() method in order to print the Employee in a nicely formatted string (notice the keyword override in the method declaration). The Employee class did not specifically inherit from any class. So by default, it inherits from the Object class, and we are able to override the ToString() method. The CalculatePay() method calculates the Employee ’s pay (rate * hoursWorked).

    In the Manager class, on the other hand, we did not override the ToString() method because the one inherited from the Employee class is sufficient. But a Manager’s pay is different from a regular Employee’s pay. Based on that, we can override the CalculatePay() method to account for the bonus. To make this work, however, we need to declare the CalculatePay() method in the Employee class as virtual (which means it is able to be overridden). Now, when we have a reference pointing to an Employee class, it will call that Employee’s CalculatePay() method. When that reference is pointing to a Manager class, it will call the Manager’s Calculatepay() method.

    Conclusion

    Overriding and overloading are very important features in an object-oriented language like C#. When used correctly, they can lead to very powerful and readable code. When we have methods with similar functionality, it is not necessary to give them different names when we can overload them instead. And when we are not satisfied with the functionality provided in a parent class, we can override the method to meet our needs.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - 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...





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