Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2006, 09:16 PM
Registered User
 
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to CodePinkDeb
Default Virtual Methods

Good Evening,

Has anyone here worked with Virtual Methods? I am transitioning from VB application development and have not worked with this before. I understand that these methods are run-time compiled and are overridden. If you look at the MSDN example, it's pretty clear how to use them.

What I don't quite understand is why would you have a base class implementation (area = x * y) and then override the function several different ways? Is it to provide a shell that you know you are going to need but really don't know which way until run-time?

Also, could you say that a Virtual Method is dynamically implemented?

Thanks for you help!



CodePinkDeb
 
Old March 14th, 2006, 10:22 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Virtual methods and classes are useful for implementing inheritance and polymorphism.

There are three basic types of inheritance in Object Oriented Programming:

An abstract overridable routine:
the derived class inherits the routine's interface but NOT it's implementation.
In C# we use the "abstract" keyword or interfaces.

An overridable routine:
the derived class inherits the routine's interface, implementation, and is also allowed to override.
In C# we use the "override" and "virtual" keywords.
Note: methods are non-virtual by default in C#.

A non-overridable routine:
the derived class inherits the routine's interface, implementation and is NOT allowed to override.
In C# we use the "sealed" keyword.

Note: I'm referring to routines as either a function or procedure.
Functions include: methods, properties, and events in C#.
Procedures are functions that return nothing (void functions).

Simplified:
We obtain polymorphism through inheritance.
We can obtain inheritance and polymorphism through the "virtual" keyword.

Links of interest:
Introduction to inheritance, polymorphism in C#: http://www.codeproject.com/csharp/csharpintro01.asp
Polymorphism (computer science): http://en.wikipedia.org/wiki/Polymor...ter_science%29

- Adam Kahtava [http://adam.kahtava.com]
 
Old March 14th, 2006, 10:31 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

An addition to my last post...

polymorphism is often used to impliment Design Patterns.

Some Design Patterns that use polymorphism are:
    The State Pattern
    Decorator Pattern
    Proxy Pattern
    Iterator Pattern
    Chain of Responsibility Pattern
    ...

See the following link for more information on Design Patterns:
Design pattern (computer science): http://en.wikipedia.org/wiki/Design_...ter_science%29

- Adam Kahtava [http://adam.kahtava.com]
 
Old March 15th, 2006, 06:37 PM
Registered User
 
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to CodePinkDeb
Default

Thank you Adam
 
Old April 29th, 2006, 08:17 AM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's an example that could simplify understanding:

class A {
    virtual void DoSomething() { // code }
}

class B : A {
    override void DoSomething() { // different code }

class C {
    void someMethod()
    {
        A aObject = new B();
        aObject.DoSomething();

At this point, one might think that because the reference is for an object of class A, that it would call A's method -- but it doesn't - -because it is declared virtual, there is special code added in the class to handle this situation, and instead, the DoSomething() method from class B is called, because the object is ACTUALLY of class B.

The pointer to A can point to B as well, because A is the base class of class B -- so "aObject" is both of class A and class B, but most specifically of class B, so that's the method that gets called.

I don't know if you got this situation from the other reply, but thought I would mention it just in case, since you're coming from VB -- java and C++ programmers have this down cold already.

-- RB






Similar Threads
Thread Thread Starter Forum Replies Last Post
Static methods spinjob BOOK: Professional C++ 7 March 12th, 2009 02:16 PM
METHODS magagulad Java GUI 3 May 15th, 2007 12:53 PM
Dispose() methods bpevangelista BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 2 May 12th, 2007 11:15 PM
What's the difference between these two methods? aaaa0441 Beginning PHP 2 January 18th, 2007 09:12 AM
Please help me with web methods DiegoF .NET Web Services 3 April 10th, 2004 11:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.