Multiple Inheritance Example c#


    The following example program in c# implements multiple inheritance in .net with interfaces. Multiple inheritance in .NET framework cannot be implemented with classes, It can only be implemented with interfaces.


What is inheritance
Different Types of Inheritance

Example program for Multiple Inheritance



using System;

 

//Example Program for Multiple Inheritance

namespace ProgramCall

{

 

    //The Icar interface should defines all car properties

    interface Icar

    {

        int WheelsCount();

 

    }

 

 

 

    //The IPlane interface should defines all plane properties

    interface IPlane

    {

        bool CanFly

        {

            get;

        }

 

    }

 

 

 

    //The superCar class should implement Icar and Iplane interfaces to become supercar

    class SuperCar : Icar, IPlane

    {

 

        //The class should implement both intefaces

        #region Icar Members

 

        public int WheelsCount()

        {

            return 4;

        }

 

        #endregion

 

 

 

 

        #region IPlane Members

 

        public bool CanFly

        {

            get

            {

                return true;

            }

 

        }

 

        #endregion

    }

 

 

 

 

 

    class Program

    {

        static void Main(string[] args)

        {

            //Creating instance for SuperCar class

            SuperCar mysupercar = new SuperCar();

 

            Console.WriteLine("My Super Car has " + mysupercar.WheelsCount() + " Wheels and can fly is " + mysupercar.CanFly);

 

            Console.ReadLine();

        }

 

    }

}

 

 

//Output

//My Super Car has 4 Wheels and can fly is True


 Output

My Super Car has 4 Wheels and can fly is True



inheritance example c#.net multiple inheritance c# example multiple inheritance example