eBusiness Help
Find 30 Outlook add-ins that can make a difference!
Save time and be more productive with our line of Microsoft Outlook add-ins.
High-volume discounts for translation services
By combining advanced technology, automated processes and volume discounts, localization is now not only affordable, but it can positively impact ROI.
Save Time With Online Invoicing, Web Timesheets & Support Ticketing
Sign up for no expiry trial account today

Free Software Archive:
Enterprise and Home Networking Downloads

Recent Articles

Virtual Methods & Polymorphism in C#
Virtual methods allow object oriented languages to express polymorphism.This means that a derived class can write a method with the same signature as a method in its base class...

Intel Releases New C++ and Fortran Compilers
Intel has announced some new tools for software developers to aid with building threaded applications, and get the most out of apps on multi-core platforms. The company claims that the new Intel Compilers...

08.18.05


Virtual Methods & Polymorphism In C#

By Pawan Bangar

Virtual methods allow object oriented languages to express polymorphism.This means that a derived class can write a method with the same signature as a method in its base class, and the bas class will call the derived class's method.By default in java, all methods are virtual.

In C# like c++, the virtual keyword is needed to specify that a method should override a method (or implementaion an abstract method) of its base class.

Class B {
public virtual void foo () {}

}
ClassD : B {
public override void foo () {}
}


Attemting to override a non-virtual method will results in a compile-time error unless the "new" keyword is added to the declaration, indicating the method is intentionally hiding the base clas's method.

Easily Create Online Help. And Online Anything Else.
Free 20-Day Trial - Click Here

Class N : D {
public new void foo () {}
}
N n = new N ();n.foo; // calls N's foo ((D)n).foo; // calls D's foo
((B)n).foo(); //calls D's foo


In contrast to both c++ and Java, requiring the override keyword makes it more clear as to what methods are overridden when looking at source code.However, requiring the use of the virtual methods has its pros and cons. The first pro is the slightly increased execution speed from avoiding virtual methods.The second pro is to make clear what methods are intended to be overridden.However, this pro can also be a con.Compare the default option of leaving out a final modifier in Java Vs leaving out a virtual modifier in C++. The default option in Java may make your program slightly less efficient, but in C++ it may prevent extendibility, albeit unforeseen, by the implementer of the base class.


About the Author:
Presently working in the capacity of the Technical Director of Birbals, an e-consulting firm based in India dedicated in providing innovative and effective web, Hospitality and consultancy services to public, private and non-profit organizations. Successfully founded Birbals and other IT companies like Ebirbals and SeoBirbals.

About CProgrammingTrends
A collection of articles and tutorials designed to help C and C variant programmers in their programming work. C Programming Tutorials and advice

CProgrammingTrends is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagmentNews.com


-- CProgrammingTrends is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2005 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal


archives | advertising info | news headlines | free newsletters | comments/feedback | submit article
C Programming Tutorials and advice CProgrammingTrends News Archives About Us Feedback CProgrammingTrends Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact