Sponsored Ad

Thursday, October 22, 2009

Interfaces in C#

Interfaces in C # are offered as a replacing for multiple inheritance. Because C # does not support multiple inheritance, it is required to incorporate some other method for that class can inherit the behavior of more than one class, avoiding the trouble of ambiguity in the name found in C + +. With the ambiguous name, the object of a class does not know what method to call if the two forms of base class object that contains the method of the same name.

Classes in C # can now use the keyword "interface" to inherit over two behavior from different interfaces. When a class inherits from two or more interfaces, they say that class is the implementation of the interface (s). The most important thing to remember about interfaces is that classes can only implement the methods defined in the interface because in C #, an interface is a built-in keyword that declares a reference type that includes technique declarations. In addition to methods, interfaces can define properties, indexers, & events that will be discussed later in this article.

An interface can inherit from one or more base interfaces. When a base type list contains a base class and interfaces, the base class must come first in the list.

interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation:
    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }

    static void Main()
    {
        // Declare an interface instance.
        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

Implementing Interfaces

First, consider the following example, the concept clearer. As we know that mammals have similar characteristics and different, so that in this example, we have two sub-classes of mammals: humans and whales. Because man is the only subclass that has the characteristic of intelligence that distinguishes it from other subclasses of mammals, Human class inherits both the class of mammals and an interface that describes how IIntelligent selectively separated from the other kinds of mammals.

using System;

namespace ConsoleApplication1
{
   public class Mammal
   {
      protected string Characteristis;
      public string characteristics
      {
         get
         {
            return this.Characteristis;
         }
         set
         {
            this.Characteristis=value;
         }
      }
   }
   interface IIntelligence
   {
      /// Interface method declaration
      bool intelligent_behavior();
   }

   class Human: Mammal, IIntelligence
   {
      public Human()
      {
          characteristics = "Human are mammals";
      }

/// Interface method definition in the class that implements it
      public bool intelligent_behavior()
      {
         Console.WriteLine("{0} and have intelligence",characteristics);
         return true
      }
   }
   class Whale: Mammal
   {
      public Whale()
      {
         characteristics = "Whale are mammals";
         Console.WriteLine("{0}",characteristics);
      }
   }
   class InterfaceApp
   {
      public static void Main(string[] args)
      {
         Whale whale = new Whale();
         Human human = new Human();
         /// The human object is casted to the interface type
         IIntelligence humanIQ = (IIntelligence)human;
         humanIQ.intelligent_behavior();

         Console.Read();
      }
   }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers