IT Community - Software Programming, Web Development and Technical Support

How do you create multiple inheritance in c# and .NET?

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?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-15-2007, 07:25 PM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 803
vadivelanvaidyanathan is on a distinguished road
Exclamation How do you create multiple inheritance in c# and .NET?

How do you create multiple inheritance in c# and .NET?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2007, 04:56 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Thumbs up Re: How do you create multiple inheritance in c# and .NET?

Dotnet does not support multiple inheritance using classes

This can be accomplished by Interface

Last edited by kingmaker : 07-20-2007 at 05:00 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-20-2007, 07:49 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: How do you create multiple inheritance in c# and .NET?

how can i implement the interface to achive the multiple inheritance ?
could you pls explain with example... ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-20-2007, 07:54 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: How do you create multiple inheritance in c# and .NET?

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 04:04 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0