in

Chennai .Net User Group

A platform that enables you to Learn, Share & Grow (India's first .Net user group)
Latest post 11-30-2008 4:12 AM by heman_1978. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 10-27-2008 10:44 AM

    • RamSelva
    • Top 50 Contributor
    • Joined on 10-27-2008
    • Posts 2
    • Points 55

    Virtual vs Abstract

    hello friend

                   In Real time appln. based on which parameter do we need to opt either Virtual or Abstract function in C#?I just want to know the diff b/w virtual/override and abstract/override?Any diff other than that abstract can't be instantiated..

  • 10-28-2008 1:50 PM In reply to

    • Shafeer
    • Top 25 Contributor
      Male
    • Joined on 10-24-2008
    • Chennai
    • Posts 5
    • Points 55

    Re: Virtual vs Abstract

    RamSelva:

    hello friend

                   In Real time appln. based on which parameter do we need to opt either Virtual or Abstract function in C#?I just want to know the diff b/w virtual/override and abstract/override?Any diff other than that abstract can't be instantiated..

     Hello RamSelva,

    Virtual functions can have implementation but Abstract function cannot have implementation

    Sample Code  

    public abstract class Base{  

       public virtual double virtualMethod() {

          return 0;

      }

    public abstract string abstractMethod {

    // Should Have Implementation in the derived class

         get;

      }

    }

    public class Derived: Base {  

        public override string abstractMethod {  

             get { return "abstractMethod"; }

         }

    }

     public class Test {  

      public static void main() {  

         Derived d = new Derived();

         d.abstractMethod // Returns "abstractMethod" 

         d.virtualMethod(); //(Returns 0 )if the

    //virtual method is overiden in the derived class it will call the overiden method only

    }

    }

     

    Filed under:
  • 11-27-2008 6:16 AM In reply to

    • yjagota
    • Top 50 Contributor
    • Joined on 11-27-2008
    • Posts 2
    • Points 25

    Re: Virtual vs Abstract

    Virtual functions can be override'n in the derived class. If you do not override them, the base implementation is called. Abstract functions on the other hand must be override'n in the derived class. If not, compiler will throw a error.

  • 11-30-2008 4:12 AM In reply to

    Re: Virtual vs Abstract

    Hi RamSelva,

    I guess there is one more part to this question that had remained unanswered. That is what will decide whether a base class is going to have a virtual function or abstract function. From my experience this is goingto be the REQUIREMENT. While designing the framework, you need to determine this. Let us say for example you have a Reporting FrameWork to implement. Assuming your primary database that has the content for reporting the you can write function called GetDBConnection which will always create or get an active connection to database. But in reporting functionality, the datasource can change to excel, oracle and so on. so this function can be Virtual. However there may be function to format the report. This function will need to be present in all classes but cannot have generic implementation. Hence it should be an abstract.

    Hope this helps

    Thanks and regards

    Venkatesh. S

     

     

    Filed under:
Page 1 of 1 (4 items)
Copyright © 2002-2008 Chennai .Net User Group. All Rights Reserved. Microsoft and Microsoft logo's are trademarks of Microsoft Corporation