Posts filed under 'c# programs'

Interface

Interface:

An Interface is a reference type and it contains only abstract members. Interface’s members can be Events, Methods, Properties .But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can’t contain constants, data fields, constructors, destructors and static members. All the member declarations inside interface are implicitly public.

Interfaces in C# are provided as a replacement of multiple inheritance. Because C# does not support multiple inheritance, it was necessary to incorporate some other method so that the class can inherit the behavior of more than one class, avoiding the problem of name ambiguity that is found in C++. With name ambiguity, the object of a class does not know which method to call if the two base classes of that class object contain the same named method.

A very important point to be remembered about c# interfaces is, if some interface is inherited, the program must implement all its declared members. Otherwise the c# compiler throws an error

 

Objective:

To create an interface and implement it in different classes

Steps to follow;

·       Create an interface iflight and define one method fly()

·       Create three classes bird,aeroplane and superman and implement

the method fly() in these classes

·       Create a main class and create an interface array of instances of the above three classes.

·       Run a loop and call the fly() method of different objects

Code:

//this is an interface with a method called fly();

    public interface IFlight

    {

        //definition of a method fly()

 

        void Fly();

       

    }

    //implementing the interface in class bird

    class Bird:IFlight

    {

 

        #region IFlight Members

        //implementing the abstract method fly() of the interface

 

        public void Fly()

        {

            Console.WriteLine(“Up Up High Sky”);

        }

 

        #endregion

    }

 

    //implementing the interface in class aeroplane

    class Aeroplane:IFlight

    {

        #region IFlight Members

        //implementing the abstract method fly() of the interface

        public void Fly()

        {

            Console.WriteLine(“aeroplane is flying”);

        }

 

        #endregion

    }

    //implementing the interface in class superman

    class SuperMan:IFlight

    {

        #region IFlight Members

        //implementing the abstract method fly() of the interface

        public void Fly()

        {

            Console.WriteLine(“Look that’s spiderman no its superman”);

        }

 

        #endregion

    }

 

//Main class

    class Program

    {

        static void Main(string[] args)

        {

            //creating an array of instances

 

            IFlight[] flyobjects ={ new Bird(), new Aeroplane(),new SuperMan() };

 

            //looping through the array

 

            foreach (IFlight obj in flyobjects)

            {

                obj.Fly();  //calling the fly method

           

            }

            Console.ReadLine();

        }

    }

Output :

Up Up High Sky

Aeroplane is flying

Look that’s spiderman no its superman

2 comments October 3, 2008


Calendar

November 2009
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Posts by Month

Posts by Category