This is a discussion on How do you create multiple inheritance in c# and .NET? within the C# Programming forums, part of the Software Development category; How do you create multiple inheritance in c# and .NET?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Defining an Interface: Let us look at a simple example for c# interfaces. In this example interface declares base functionality of node object. interface INode { string Text { get; set; } object Tag { get; set; } int Height { get; set; } int Width { get; set; } float CalculateArea(); } The above INode interface has declared a few abstract properties and function which should be implemented by the derived classes. //Sample for Deriving a class using a C# .Net interface - c# tutorial public class Node : INode { public Node() {} public string Text { get { return m_text; } set { m_text = value; } } private string m_text; public object Tag { get { return m_tag; } set { m_tag = value; } } private object m_tag = null; public int Height { get { return m_height; } set { m_height = value; } } private int m_height = 0; public int Width { get { return m_width; } set { m_width = value; } } private int m_width = 0; public float CalculateArea() { if((m_width<0)||(m_height<0)) return 0; return m_height*m_width; } } Now the above code has created a c# class Node that inherits from INode c# interface and implement all its members. 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. The above code was a simple example of c# interface usage. Now this has to be followed with some advanced details of interface building in C# .Net. The previous example used only names of methods or properties that have the same names as in interface. But there is another alternative method for writing the implementation for the members in class. It uses full method or property name e.g. INode.CalculateArea () {// implemetation}. Multiple Inheritance using C# interfaces: Next feature that obviously needs to be explained is multiple inheritance using c# interfaces. This can be done using child class that inherits from any number of c# interfaces. The inheritance can also happen with a combination of a C# .Net class and c# interfaces. Now let us see a small piece of code that demonstrate us multiple inheritance using only interfaces as parent class ClonableNode : INode,ICloneable { public object Clone() { return null; } // INode members } The above example created a class ClonableNode. It implements all the functionality of INode interface in the same way as it was done in Node class. Also it realizes Clone method only one item of IClonable interface of .NET library. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| i want create one web site and same site to work multiple language how is possible ? | vel.m8 | ASP and ASP.NET Programming | 1 | 11-15-2007 01:58 AM |
| simulating OOP-style inheritance in C? | prasath | C and C++ Programming | 1 | 08-18-2007 12:40 AM |
| What are some alternatives to inheritance? | prasath | Java Programming | 1 | 07-20-2007 06:49 AM |
| Why multiple inheritance cant support in java | leoraja8 | Java Programming | 2 | 07-16-2007 11:34 PM |
| Inheritance Class in C++ | vigneshgets | C and C++ Programming | 1 | 05-23-2007 12:00 PM |