Updated : Dec 31, 2021 in Articles

Abstract functions &Abstract classes

 A function created by using abstract keyword is known as abstract function.
*an abstract function contains signature only and does not contain any implementation.
*an abstract function should be terminated (;).
*overriding of an abstract function in derived class is mandatory.
*if an abstract function is not override in derived class then it raises the compile time error.
*An abstract function can be written make following members 
1. Functions
2. Properties
3. Indexes
4. Nested types.
abstract class:
a class that contains one or more abstract functions is known as an abstract class.
*To create a abstract class use abstract keyword.
Example:

abstract class Employee
    {
       protected int eid, age, sal;
       protected string ename, address;
       public abstract void getempdata();
       public abstract void dispempdata();      
    }
    class Manager:Employee
    {
        double ca, bonus;
        public override void getempdata()
        {
           Console.WriteLine("Enter employee details");
            eid = Convert.ToInt32(Console.ReadLine());
            age = Convert.ToInt32(Console.ReadLine());
            sal = Convert.ToInt32(Console.ReadLine());
            ename = Console.ReadLine();
            address = Console.ReadLine();
            Console.WriteLine("enter Manager details");
            ca = Convert.ToDouble(Console.ReadLine());
            bonus = Convert.ToDouble(Console.ReadLine());
        }
        public override void dispempdata()
        {
            Console.WriteLine("Employee id is:" + eid);
            Console.WriteLine("Employee age is:" + age);
            Console.WriteLine("Employee sal is:" + sal);
            Console.WriteLine("Employee name is:" + ename);
            Console.WriteLine("ca is:"+ca);
            Console.WriteLine("Bonus is:"+bonus);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Manager m = new Manager();
            m.getempdata();
            m.dispempdata();
            Console.ReadLine();
        }
    }

Difference between virtual and abstract functions:
virtual function
1. Contains both signature and implementation
1. Contains signature only and does,nt contain any implementation
2. Use virtual keyword to create a virtual function
2. Use abstract keyword to create a abstract function
3. overriding of virtual function is optional
3. Overriding of abstract function is mandatory
4. If virtual function is not override in derived class then base class virtual function is called.
4. If abstract function is not override in derived class then it raises compile time error.
5.virtua function can be written in abstract and non abstract classes.
5.an abstract function can be written in abstract class or in inheritance only
6.virtual function should not be terminated
6.abstract function should  be terminated ​

Latest posts by DuttaluruVijayakumar (see all)
Like
Like Love Haha Wow Sad Angry

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x