Differences between Abstract & Virtual functions -
Members
Class
Members
- Abstract function doesn't contain any body but a virtual function contain body
- We must be implement the abstract function in derived class but it is not necessary for virtual function
- Abstract function can only use in abstract class but it is not necessary for virtual function
- Abstract function are called pure virtual function
- It must be a function member. That is, fields and constants cannot be abstract members.
- You cannot create instances of an abstract class.
- An abstract class is declared using the abstract modifier
- An abstract class can contain abstract members or regular, nonabstract members.
- An abstract class can itself be derived from another abstract class
- Any class derived from an abstract class must implement all the abstract members of the class by using the override keyword, unless the derived class is itself abstract.
abstract class Helloworld
{
public int Int1 { get; set; } //Non Abstract property
public abstract int Int2 //Abstract Property
{
get;
set;
}
public Helloworld() //Constructor
{
Console.WriteLine("Helloworld Constructor");
}
public void Method1() //Non Abstract Method
{
Console.WriteLine("Helloworld Method1");
}
public abstract void Method2(); //Abstract Method
~Helloworld() //Destructor
{
Console.WriteLine("Helloworld Destructor");
}
}
class Drived : Helloworld
{
public int Val;
public override int Int2
{
get
{
return Val;
}
set
{
Val=value;
}
}
public override void Method2()
{
Console.WriteLine("Drived Method2");
}
}
class Program
{
static unsafe void Main(string[] args)
{
Drived Dr = new Drived();
Dr.Int1 = 10;
Dr.Int2 = 20;
Dr.Method1();
Dr.Method2();
Console.ReadLine();
}
}
No comments:
Post a Comment