473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I don't understand inheritance!

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.
Nov 16 '05 #1
3 1415
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

"Francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:Op*************@TK2MSFTNGP12.phx.gbl...
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.

Nov 16 '05 #2
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/de...overridepg.asp


"Francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:Op*************@TK2MSFTNGP12.phx.gbl...
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.

Nov 16 '05 #3
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
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
1
by: sapropel | last post by:
ive been trying to understand Inheritance, Polymorphism and templates, and so ive tried to make a test using all of those. it is about a base class taht receives two params on the template and two...
2
by: Carlos Martinez Garcia | last post by:
Hi all: I have a problem compiling a file and I can't understand why. Here is my code: class Base { public: virtual void foo()=0; virtual void foo(int i) {
31
by: Jo | last post by:
class A { public: char text_a; A() { *text_a=0; } ~A() {} }; //-----------------------------------------------------------------------------
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.