Can anyone explain difference between a virtual and abstract function with example in C#?


Can anyone help me to understand the difference between virtual functions and abstract functions with the example in C#, I would like to understand it in easy languages as I am not able to get it reading books etc, so I can understand in which situation I should use which function(abstract/virtual)?

Thanks


Asked by:- Vinnu
3
: 3318 At:- 12/28/2017 8:46:54 AM
C# OOPS-concept difference between abstract function and a virtual function?







2 Answers
profileImage Answered by:- bhanu

You can understand the difference between abstract function and virtual function in easy language as:

An abstract function cannot have functionality. You're basically saying, any child class MUST give their own version of this method, however, it's too general to even try to implement in the parent class.

A virtual function is basically saying look, here's the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality.

Now, read these lines to understand about the abstract/virtual functions

Abstract Function

  •  An abstract function has no implementation. It can only be declared. This forces the derived class to provide the implementation of it
  •  Abstract means we MUST override it.
  •  An abstract member is implicitly virtual. Abstract can be called as pure virtual in some of the languages.
  • An abstract modifier can be used with classes, methods, properties, indexers and events.

Virtual Function

  • Virtual Function has an implementation. When we inherit the class we can override the virtual function and provide our own logic, but it is not mandatory to override virtual methods.
  • We can change the return type of Virtual function while implementing the function in the child class(which can be said as concept of Shadowing).
  • Virtual means we CAN override it means a virtual method can be overridden or cannot be overridden by child class

C# Example of Abstract class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Abstract_Method
{
    class Program
    {
        static void Main(string[] args)
        {
            childclass ch = new childclass();
            ch.sum();
            Console.ReadKey();
        }
    }
}
 
abstract class baseclass
{
    public int num = 5;
    public abstract void sum();   
}
//child class inherited baseclass
class childclass : baseclass
{
   //childclass implemnets sum method of abstract class with abstract method
    public override void sum()
    {
        Console.WriteLine("Total Sum : " + num * num);
    }
}

Output:

Total Sum : 25

C# Example of Virtual

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Virtual_Methods
{
    class Program
    {
        static void Main(string[] args)
        {
            child1 ch1 = new child1();
            ch1.message();
 
            child2 ch2 = new child2();
            ch2.message();
 
            Console.ReadKey();
        }
    }
}
class baseclass
{
    public virtual void message()
    {
        Console.WriteLine("Base class Virtual Method");
    }
}
class child1 : baseclass
{
    public override void message()
    {
        Console.WriteLine("Child 1 class");
    }
}
class child2 : baseclass
{   
}

Output:

Child 1 class
Base class virtual method
4
At:- 12/29/2017 7:59:17 AM Updated at:- 10/5/2022 6:52:27 AM
thank you for your detailed answer 0
By : Vinnu - at :- 12/31/2017 11:06:44 AM
Virtual and Abstract difference with example, thanks 0
By : vikas_jk - at :- 10/5/2022 4:50:55 AM


profileImage Answered by:- user_1160149716

Abstract - Cannot declare its body in class it's declared, must be declared inside Abstract class only and derived class must provide implementation by using override keyword for all abstract methods

Virtual - Should declare it's body in class it's defined, can be declared inside a normal or abstract class and derived class may or may not override the virtual method as per requirement.

using System;
namespace HARSH
{
abstract class first
{
public abstract void AbstractMethod1();
public virtual void Method1()
{
Console.WriteLine("Virtual method in abstract class");
}
}
class second
{
public virtual void Method2()
{
Console.WriteLine("Virtual Method2");
}
}
class third:second
{
public override void Method2()
{
Console.WriteLine("Inside Overidden Virtual Method2");
}
}
class forth:first
{
public override void AbstractMethod1()
{
Console.WriteLine("Inside Overidden absract Method1");
}
}
public class Program
{
public static void Main(string[] args)
{
first ob1 = new forth();
ob1.AbstractMethod1();
ob1.Method1();
Console.WriteLine();
second ob2 = new third();
ob2.Method2();
}
}
}using System;
namespace HARSH
{
abstract class first
{
public abstract void AbstractMethod1();
public virtual void Method1()
{
Console.WriteLine("Virtual method in abstract class");
}
}
class second
{
public virtual void Method2()
{
Console.WriteLine("Virtual Method2");
}
}
class third:second
{
public override void Method2()
{
Console.WriteLine("Inside Overidden Virtual Method2");
}
}
class forth:first
{
public override void AbstractMethod1()
{
Console.WriteLine("Inside Overidden absract Method1");
}
}
public class Program
{
public static void Main(string[] args)
{
first ob1 = new forKALSASKLDNth();
ob1.AbstractMethod1();
ob1.Method1();
Console.WriteLine();
second ob2 = new third();
ob2.Method2();
}
}
}

2
At:- 1/7/2018 7:41:01 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use