C# Virtual Class Example

Here is an example as how to implement virutal class in c sharp
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
     class animal
    {
        public  virtual void foodhabits()
        {
            Console.WriteLine(“Animals have differnt food habits”);
        }
    }
    class carnivorous : animal
    {
        public override void foodhabits()
        {
            Console.WriteLine(“The carnivrous animal eat only meat”);
        }
    }
    class herbivorous : animal
    {
        public override void foodhabits()
        {
            Console.WriteLine(“the HB animals eat only plants”);
        }
    }
    class implement
    {
        public void callfunction(animal cr)
        {
            cr.foodhabits();
        }
    }
    class classmain
    {
        public static void Main()
        {
            {
                implement imp = new implement();
                carnivorous cn = new carnivorous();
                herbivorous hb = new herbivorous();
                imp.callfunction(cn);
                imp.callfunction(hb);
                Console.ReadLine();
            }
        }
    }
}

Add Comment