Latest Posts

Friday, January 27, 2017

Abstract and virtual function and class in C#





i always find abstract and virtual as confusing. so i decided to do all test and write my experience of the same.I have consider all the angles, where a smart interviewer can trap you.
Most of the time the first question an interviewer ask is whats difference between abstract and virtual method in C#.

The simplest difference is that , body is needed for virtual function where it is declare, where as its just opposite in case of abstract function.

Lets examine this example

The first class is abstract class which contains a abstract method inabs()
if we give body to this method then it will give compile time error saying "cannot declare a body Function is marked as abstract".

Making a function abstract compels the inheriting class to define the abstract function.



 
    public abstract class abstractparent {     
        public abstract void inabs();      
        public void noabsparent()      
        {       
            Console.WriteLine("In Class Contaning abstract methode");      
        } 
    } 
    public class abstractchild : abstractparent {     
        public void noabschild()     
        {        
            Console.WriteLine("In Child class of Abstract Parent");     
        }     
        public override void inabs()     
        {        
            Console.WriteLine("In Child class functin definition of abstract");     
        } 
    }



Also when we are declaring a abstract function in a class its necessary to use ABSTRACT keyword with class and function both.
A non abstract class can't contain the abstract method.if we defined a abstract method in non abstract class and try to compile it then it will give compile time error stating "A non abstract class has abstract function".you can check it by altering the above classes.

In virtual function we must have to specify the body of the virtual function.if we not specify the body then it will give compile time error stating "must declare a body because it is not marked as abstract, extern or partial".




#region virtual_function
    class virtual_parent
    {
        public virtual void vir_parent()
        {
            Console.WriteLine("IN vir_parent function of parent class which is defiend in parent class");
        }   
        public void non_vir_parent()
        {
            Console.WriteLine("IN non_vir_parent function of parent class which is defiend in parent class");
        }
    }
    class virtual_child:virtual_parent
    {
        new public void non_vir_parent()
        {
            Console.WriteLine("IN non_vir_parent function of CHILD class which is defiend in CHILD class");
        }
        public override void  vir_parent()
        {
            Console.WriteLine("IN vir_parent function of CHILD class which is defiend in CHILD class");
           //base.vir_parent();
        }
    }
    #endregion


Some tricky question interviewer may ask you.

1)Can we declare and defined a normal(not abstract, virtual,extern or partial) function in derived class which is already present in base class.
Yes we can do this in dotnet.
By using new keyword we can re-declare and redefine a normal function in drived class. for detail please refer the above code.

2)If above is true then how you will call the base class function by using child class object.

this we can do by explicitly type casting the base class object in to parent class

Use this code to see the result
virtual_child obj_virtual_child = new virtual_child();
((virtual_parent)obj_virtual_child).non_vir_parent();

I hope i have covered all the expect of the virtual and abstract.please let me know if any body have any query.
please also let me know if any body have problem in any concept of dotnet.
You can reach me at rajhan143-hostway@yahoo.co.in

2 comments: