Hi,
I want to know about some basic difference between abstract and virtual method. because i have to override both method in derived class.
Hi,
I want to know about some basic difference between abstract and virtual method. because i have to override both method in derived class.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vineet Kumar SainiPosted Oct 23, 2012, 8:22 AM
Only abstract classes can have abstract members.
-A non-abstract class that inherits from an abstract class must override its abstract members.
-An abstract method is implicitly virtual.
-An abstract method cannot provide any implementation.
- A class can have a virtual method.
-The virtual method has an implementation. When you inherit from a class that has a virtual method, you can override the virtual method.
Please visit http://softwarecafeblog.blogspot.in/2011/05/abstract-vs-virtual-in-c.html for more details.
Shivanand ArurPosted Oct 23, 2012, 8:18 AM
Virtual Keyword :
Basically the "Virtual" keyword when you want your method in the Parent Class to be used in the Derived Class. But it is not mandatory/compulsory to override the Virtual Method. It depends upon the developer, whether he/she wants to override that method. The Execution of the Virtual Method is based at Runtime.... (Whether the Child Instance is calling the Virtual Method or the Parent Instance is calling, this is decided at Runtime)
Few more points to remeber -
1. A Virtual Keyword is used to specify a method as Virtual
2. The Derived Class should use the "Override" keyword to actually use that Virtual Method.
3. A Virtual Method can never contain an "Override" Keyword
for example -
Class Vehicle
{
public virtual void Tyres()
{
Console.WriteLine("A Vehicle has Tyres");
}
}
Class Car : Vehicle
{
public override void Tyres()
{
Console.WriteLine("A Car has 4 Tyres");
}
}
Class MyClass
{
public static void Main()
{
Car myCar = new Car();
myCar.Tyres(); // Child Instance is calling the Virtual Method
/*
Now here the Tyre Method is derived by the Child Class Object, which is basically decided at the Runtime.
*/
}
}
Abstract Keyword:
A method which is defined with the keyword "Abstract" is basically Abstract. It does not contain any implementation. It is just defined in the Parent Class. The Class which derives the Parent Class, has to mandatorily implement the Abstract Method. Further, the Abstract method is internally Virtual, its just that we do not define the keyword Virtual... But when we use the keyword "Abstract" with a method, the Compiler automatically takes that method as Virtual. An abstract method does not contain any implementation in its body.
for example :
Class Vehicle
{
public abstract void Tyres();
}
Class Car : Vehicle
{
public override void Tyres()
{
Console.WriteLine("A Car has 4 Tyres");
}
}
Class MyClass
{
public static void Main()
{
Car myCar = new Car();
myCar.Tyres();
}
}
Hope you understood the Concept. And one last point.... Using these concepts completely depends upon your requirement!!!
Please Mark the Answer as Accepted if it Helped!!!
Thank you!!!
Santhosh Kumar JayaramanPosted Oct 23, 2012, 7:47 AM
public abstract string GetAbstractValue();
public virtual string GetVirtualValue()
{
return "as";
}
Its not mandatory to override a virtual, but abstract should be overridden