Crosspost (Inheritance and Interfaces)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Crosspost (Inheritance and Interfaces)

  1. #1
    Vlad Ivanov Guest

    Crosspost (Inheritance and Interfaces)

    I am just starting with C# (i guess same prinicples apply to VB.Net ) so i
    don't know how things work very well, but...

    Let me add a query to the knowledgable: If class A Inherits a class B that
    implements multiple interfaces - does my class A, now based on class B
    automatically also implements the interfaces of the base B?

    To illustrate: (pseudocode)

    CB implements IC and ID
    CA inherits CB

    Can CA be cast to IC and ID?

    And if it can, when i call a method defined by IC that was implemented by CB
    (and in turn inherited by CA) then the code executes in CB? Right?

    Thanks guys



  2. #2
    Rob Teixeira Guest

    Re: Crosspost (Inheritance and Interfaces)


    The following is a quote from the C# language guide. Hope it helps.

    -Rob

    A class inherits all interface implementations provided by its base classes.

    Without explicitly re-implementing an interface, a derived class cannot in
    any way alter the interface mappings it inherits from its base classes. For
    example, in the declarations

    interface IControl
    {
    void Paint();
    }
    class Control: IControl
    {
    public void Paint() {...}
    }
    class TextBox: Control
    {
    new public void Paint() {...}
    }

    the Paint method in TextBox hides the Paint method in Control, but it does
    not alter the mapping of Control.Paint onto IControl.Paint, and calls to
    Paint through class instances and interface instances will have the following
    effects

    Control c = new Control();
    TextBox t = new TextBox();
    IControl ic = c;
    IControl it = t;
    c.Paint(); // invokes Control.Paint();
    t.Paint(); // invokes TextBox.Paint();
    ic.Paint(); // invokes Control.Paint();
    it.Paint(); // invokes Control.Paint();

    However, when an interface method is mapped onto a virtual method in a class,
    it is possible for derived classes to override the virtual method and alter
    the implementation of the interface. For example, rewriting the declarations
    above to

    interface IControl
    {
    void Paint();
    }
    class Control: IControl
    {
    public virtual void Paint() {...}
    }
    class TextBox: Control
    {
    public override void Paint() {...}
    }
    the following effects will now be observed

    Control c = new Control();
    TextBox t = new TextBox();
    IControl ic = c;
    IControl it = t;
    c.Paint(); // invokes Control.Paint();
    t.Paint(); // invokes TextBox.Paint();
    ic.Paint(); // invokes Control.Paint();
    it.Paint(); // invokes TextBox.Paint();

    Since explicit interface member implementations cannot be declared virtual,
    it is not possible to override an explicit interface member implementation.
    It is however perfectly valid for an explicit interface member implementation
    to call another method, and that other method can be declared virtual to
    allow derived classes to override it. For example

    interface IControl
    {
    void Paint();
    }
    class Control: IControl
    {
    void IControl.Paint() { PaintControl(); }
    protected virtual void PaintControl() {...}
    }
    class TextBox: Control
    {
    protected override void PaintControl() {...}
    }

    Here, classes derived from Control can specialize the implementation of IControl.Paint
    by overriding the PaintControl method.



    "Vlad Ivanov" <vivanov@polarisconsulting.com> wrote:
    >I am just starting with C# (i guess same prinicples apply to VB.Net ) so

    i
    >don't know how things work very well, but...
    >
    >Let me add a query to the knowledgable: If class A Inherits a class B that
    >implements multiple interfaces - does my class A, now based on class B
    >automatically also implements the interfaces of the base B?
    >
    >To illustrate: (pseudocode)
    >
    >CB implements IC and ID
    >CA inherits CB
    >
    >Can CA be cast to IC and ID?
    >
    >And if it can, when i call a method defined by IC that was implemented by

    CB
    >(and in turn inherited by CA) then the code executes in CB? Right?
    >
    >Thanks guys
    >
    >



  3. #3
    Vlad Ivanov Guest

    Re: Crosspost (Inheritance and Interfaces)

    Oh thanx so much!

    "Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
    news:3a3a6cc2$1@news.devx.com...
    >
    > The following is a quote from the C# language guide. Hope it helps.
    >
    > -Rob
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center