Interface

October 3, 2008

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

Entry Filed under: c# programs. Tags: , , , .

2 Comments Add your own

  • 1. rahul  |  October 3, 2008 at 3:16 pm

    remote comment…

    ckeck the status.

    Reply
  • 2. tarun  |  October 3, 2008 at 6:05 pm

    Very nice initiative by Rahul. This will definitely help us in future when we will be needing these codes in reality.:P
    The next entry will be from my side

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

October 2008
M T W T F S S
     
 12345
6789101112
13141516171819
20212223242526
2728293031  

Most Recent Posts