Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
when do we use abstract class/methods and when do we use virtual keyword in C# wrt to Dynamic Polymorphism?
Please explain with a scenario

Regards,
VJ
Posted
Updated 2-Aug-14 19:06pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Aug-14 1:33am    
Showing the usage won't help you much. You need to understand the code mechanism of OOP, how dynamic dispatch based on virtual functions work. "When do we use" question is not something certain. "We" use it when we need late binding. It's a big design question when we need late binding.
—SA
Kishor Deshpande 3-Aug-14 2:32am    
Well, to know this you should learn the concept first, you can google and get good examples :)

What about reading the documentation Polymorphism (C# programming guide)[^] ?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Sep-14 22:43pm    
5ed.
—SA
CPallini 13-Sep-14 1:53am    
Thank you.
Abstract classes are used when there is a need to implement functionality in some methods, but leave others to be implemented by other classes.
Virtual is used when a method has functionality but other classes may choose to use this or create their own.

For e.g (from msdn[^]).
C#
abstract class WashingMachine
{
   public WashingMachine()
   {
      // Code to initialize the class goes here.
   }

   public virtual void Wash()
   {
      //Wash clothes
   }
   abstract public void Rinse(int loadSize);
   abstract public long Spin(int speed);
}


Here all machines was but spin and rinse is left for other classes to implement.
 
Share this answer
 
You use abstract classes when you want to leave some functionality to be implemented by the derived classes. For an example, consider the following code,
C#
abstract class DrawingHelper
{
  // LoadModel and SaveToFile are the same regardless of the drawing API.
  void LoadModel(string fileName)
  {
    ...
    ...
  } 
  void SaveToFile(string fileName)
  {
    ...
    ...
  }
  // This method is specific to drawing API.
  abstract public void Draw(int x, int y, int z);
}

class OpenGLDrawingHelper : DrawingHelper
{
  public override void Draw(int x, int y, int z)
  {
    ...
    ...
  }
}

class DirectXDrawingHelper : DrawingHelper
{
  public override void Draw(int x, int y, int z)
  {
    ...
    ...
  }
}

Thus the method "Draw" differs according to the drawing API. Consider the following code,
C#
DrawingHelper dHelper;
// This is a pseudocode. The enum APIChoice is imaginary. Assume that "choice" is changed by the user input in your UI.
if(choice == APIChoice.OpenGL)
{
  dHelper = new OpenGLDrawingHelper();
}
else if(choice == APIChoice.DirectX)
{
  dHelper = new DirectXDrawingHelper();
}
dHelper.LoadModel("D:\\car.b3d");
dHelper.Draw(100, 200, 300);

The main difference between an abstract and a virtual method is you can't implement an abstract method in the base class. But you can implement a virtual function in the base class.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Sep-14 22:43pm    
Comparison classes and method, whatever they are, and telling "differences", is worse than comparing apples and pears. It's like comparing bees with hives.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900