|
-
May 4th, 2004, 04:28 PM
#1
Thread Starter
Addicted Member
multiple inheritance
i was doing some research on multiple inheritance and i found that interesting article though i should post it for others reference
----------------------------------------------------------------------------------
Multiple inheritance Classes created in Visual Basic and Visual C# can have only one
base class, but they can implement multiple interfaces. Because interfaces behave
polymorphically, like base classes, you can use interfaces to simulate multiple
inheritance. Suppose you were creating a Backyard class and wanted it to derive from
both Lawn and Garden, but Lawn and Garden didn’t share a common base class other
than System.Object. You could choose to implement an ILawn interface and then
implement the ILawn interface in a Lawn class. You then create Garden as a base class.
When you create the Backyard class, it inherits from Garden and implements ILawn as
you see here:
‘ Visual Basic
Public Class Backyard
Inherits Garden
Implements ILawn
End Class
// Visual C#
public class Backyard : Garden, ILawn {
}
It would appear that you haven’t gained much from this code because you have to
reimplement all the members of ILawn. Fortunately, you can use containment and
delegation to reuse some of your work. In the Backyard class, you can create a private
instance of the Lawn class. This is containment. You then implement the ILawn methods
by calling the corresponding method of the private Lawn instance. This is called
delegation. You’re delegating the work of the ILawn interface to the contained Lawn
member. Suppose the ILawn class has a Grow method and a Height property. Your code
might look something like this:
‘ Visual Basic
Public Class Backyard
Inherits Garden
Implements ILawn
Private m_lawn As New Lawn()
Public Sub Grow() Implements ILawn.Grow
m_lawn.Grow()
End Sub
Public Property Height() As Integer Implements ILawn.Height
Get
Return m_lawn.Height
End Get
Set(ByVal Value As Integer)
m_lawn.Height = Value
End Set
End Property
End Class
// Visual C#
public class Backyard : Garden, ILawn {
private Lawn m_lawn = new Lawn();
#region Implementation of ILawn
public void Grow() {
m_lawn.Grow();
}
public int Height {
get { return m_lawn.Height; }
set { m_lawn.Height = value; }
}
#endregion
}
Last edited by tolisss; May 4th, 2004 at 04:40 PM.
-
May 4th, 2004, 05:57 PM
#2
PowerPoster
HI,
But isn't this the same as simply creating an instance of the implemented class and then calling or referencing the method or property of that class?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 4th, 2004, 06:04 PM
#3
Thread Starter
Addicted Member
maybe it is i m not so experience yet to tell you why this is suggested on the ebook i m reading i just did some search in the forum and nobody mention that alternative so though i post it for you to think about it
ps: that is from ebook :
(Ms Press) OOP with Microsoft Visual (Basic & C# ) .NET Step by Step
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|