Results 1 to 3 of 3

Thread: multiple inheritance

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    didn't decide yet
    Posts
    222

    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.
    Come and get our ISDN CallerID http://www.3wm.biz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width