Interface-OOPS

 

 

// Interface

 

    // Interfaces, like classes, define a set of properties, methods, and events.

    // But unlike classes, interfaces do not provide implementation. An interface represents

    // a contract, and a class that implements an interface must implement every aspect of that

    // interface exactly as it is defined. You declare an interface by using the interface keyword

 

    interface IPerson

    {

        void Eat();

 

        void Sleep();

 

        int Weight

        {

            set;

            get;

        }

    }

 

    // In order to use this interface, you must declare your class in the same way that

    // you declare a class inheriting from any other object.

 

 

    public class Man : IPerson

    {

        int iWeight;

 

        public Man()

        {

        }

 

        public void Eat()

        {

            Console.WriteLine("Man:Eat");

        }

 

        public void Sleep()

        {

            Console.WriteLine("Man:Sleep");

        }

 

        public int Weight

        {

            set

            {

                iWeight = value;

            }

            get

            {

                return iWeight;

            }

        }

        static void Main()

        {

            Man i = new Man();

            i.Eat();

            i.Sleep();

 

            Console.ReadLine();

        }     

 

    }

 

 

 

        // You get the following result when you run the above code.

        // Man:Eat

        // Man:Sleep

 

        // It is important to note here that an interface is very different from a base class.

        // An interface is implemented, not extended.

 

        // 1.A class can implement multiple interfaces.

        // 2.An interface cannot contain data declarations but you can declare properties.

        // 3.All method declarations in an interface are public.

        // 4.There can be no implementation in an interface.

        // 5.The class implementing the interface must provide implementation code.

        // 6.An interface can be derived from another interface.