Thursday, July 10, 2014

Multiple inheritance in C#

When we are talking about Object Oriented Programming concepts inheritance is most important concept. C# is that language derived from powerful lanuage C++. But some of the features in C++ such like Multiple inheritance is not allowed with C#.

Instead of multiple inheritance with Classes C# provide multiple inheritance with interfaces. If you have scenario that multiple inheritance need to play role you can go with interfaces with one base class. 

An interface contains only the signatures of methods, properties, events or indexers. A class or Struct that implements the interface must implement the members of the interface that are specified in the interface definition.

You can inherit any number of interfaces but only one base class


public interface demoInterface
   {
       public string interfaceProperty { get; set; }
       public void interfaceMethod();
   }
   public class DemoClass1
   {
       public int demoClassProp1 { get; set; }
   }
   public class DemoMultipleInheritClass:DemoClass1,demoInterface
   {
       private string Property;
       public string interfaceProperty  // read-write instance property
       {
           get
           {
               return Property;
           }
           set
           {
               Property = value;
           }
       }
       public void interfaceMethod()
       {
           int variable1 = this.demoClassProp1;  
       }
   }



0 comments: