I don't understand inheritance!

M

Matt Berther

Hello francois,

Look up explicit interface implementation to answer the majority of your
questions.

In theory, the way to get around this is to provide a virtual method that
the explicit implementation calls, which your subclasses can override. However,
it looks like the ASP.NET team did not do that here.
 
S

Stefan Simek

I guess it's something that has been overlooked in the 1.1 framework.

In 2.0, these methods are declared as protected virtual, so you don't need
to reimplement the interface, just override them if you want to.

HTH,
Stefan
 
O

Ollie

you can't override Interface methods that aren't marked as
virtual\abstract...

e.g.

public abstract baseCondition : ICloneable
{
public abstract void DoSomething();
public abstract object Clone();
}

public SimpleCondition : BaseCondition
{
public override object Clone()
{
....
}

public override void DoSomething()
{
....
}
}

You might what to chekc out the 'new' keyword as in:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfoverridepg.asp
 
M

Matt Gerrans

I forget all the rationale behind this, because it was a while ago that I
read the material (when I was first learning C#), but the problem is that if
the base class chose to implement the interface methods with the
InterfaceName.method() signature, they cannot have access modifiers and
cannot be virtual. If you have this interface:

interface IFace
{
string foo();
}
You could do this to satisfy it:
class Base1 : IFace
{
public virtual string foo() { return "virtual foo from Base1"; }
}
and then you could override it:
class Derived1 : Base1
{
public override string foo() { return "virtual foo from Derived1"; }
}
but if you did this:
class Base2 : IFace
{
string IFace.foo() { return "IFace.foo from Base2"; }
}
You would satisfy the interface, but could not add the public and virtual
modifiers, so you couldn't override it. You can also do both:
class Base3 : IFace
{
public virtual string foo() { return "virtual foo from Base3"; }
string IFace.foo() { return "virtual foo from Base1"; }
}
And in this case, you can override the virtual foo().

Anyway, it looks like in your case, the CheckBox class has implemented the
interface as in case 2, so the designer of that class didn't plan for you to
override those methods.

- Matt
 
F

Francois

Hi,

I am trying to extend a .NET class for overriding some methods.

The class that I try to override is : System.Web.UI.WebControls.CheckBox.
Its full definition is : public class CheckBox : WebControl,
IPostBackDataHandler (notice the interface that is implemented)

I want to override 3 methods belonging to that class:

1. protected virtual void OnCheckedChanged(EventArgs e) //Inherited from
the class itself
2. IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection
postCollection) // defined in the IPostBackDataHandler interface
3. IPostBackDataHandler.RaisePostDataChangedEvent() // defined in the
IPostBackDataHandler interface

Methods 2 and 3 are not defined by the class CheckBox class itself but as
the class implements the interface IPostBackDataHandler then it must
implements those 2 methods.
Then a first question arise is: Why those 2 methods do not appear in the
class reference of the CheckBox? Should the reference not display all
methods that are implemented by the interface? Or can a class implements an
interface and make the methods defined in the interface private?

Anyway I try to extends the checkBox class with the following code but it
does not compile:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI;

namespace Bos.CustomControls
{
public class CheckBoxExtension : System.Web.UI.WebControls.CheckBox
// i also tried like this but it does not work neither:
// public class CheckBoxExtension : System.Web.UI.WebControls.CheckBox,
IPostBackDataHandler
{
public CheckBoxExtension()
{
}

protected override void OnCheckedChanged(EventArgs e)
{
//this method compiles
base.OnCheckedChanged(e);
}

override bool IPostBackDataHandler.LoadPostData(string postDataKey,
NameValueCollection postCollection)
{
//this does not compile, compiler says: The modifier 'override' is not
valid for this item
return true;
}

override void IPostBackDataHandler.RaisePostDataChangedEvent()
{
//this does not compile, compiler says: The modifier 'override' is not
valid for this item
}
}
}

To make the code compile and work, I have to do like the following:

public class CheckBoxValue : System.Web.UI.WebControls.CheckBox,
IPostBackDataHandler
{

protected override void OnCheckedChanged(EventArgs e)
{
// here i can override a method inherited from the base class
}

bool IPostBackDataHandler.LoadPostData(string postDataKey,
NameValueCollection postCollection)
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHandler. I have a
to re implement the interface like if it was never implemented by the base
class. I do not use the override keyword
*/
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHandler. I have a
to re implement the interface like if it was never implemented by the base
class. I do not use the override keyword
*/
}
}

For me, this is a problem for 2 reasons:
- First, I cannot call the method base.LoadPostData neither
base.RaisePostDataChangedEvent. What about the case I would like to keep the
default behavior of those methods but just adding some extra functionality
that I need for my app? When pple override a method, it can very likely be
for adding a new functionality, not to replace the functionality.
- Second, as those methods must exist (as the base class implements the
interface) why can i not override them? And what I am doing in the second
code that compiles seems to me more like bypassing the implementation of the
interface of the base class and re-implement it again int the derived class.
Like if the interface was never implemented at all at first! This seems a
little bit dangerous as people can reimplement interfaces inherited by their
base class without knowing it! And without having to use the "override"
keyword. Thus it may bring buggy code! Also it just does not make sense to
me, if a base class implements this or that interface, how comes I can
reimplement it again in a derived class like if it was never implemented at
the first place?

I would really appreciate if someone could enlight me on this.

Tx a lot in advance,
Best regards,

Francois.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top