473,769 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.W ebControls.Chec kBox.
Its full definition is : public class CheckBox : WebControl,
IPostBackDataHa ndler (notice the interface that is implemented)

I want to override 3 methods belonging to that class:

1. protected virtual void OnCheckedChange d(EventArgs e) //Inherited from
the class itself
2. IPostBackDataHa ndler.LoadPostD ata(string postDataKey, NameValueCollec tion
postCollection) // defined in the IPostBackDataHa ndler interface
3. IPostBackDataHa ndler.RaisePost DataChangedEven t() // defined in the
IPostBackDataHa ndler interface

Methods 2 and 3 are not defined by the class CheckBox class itself but as
the class implements the interface IPostBackDataHa ndler 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.Collecti ons;
using System.Collecti ons.Specialized ;
using System.Web.UI;

namespace Bos.CustomContr ols
{
public class CheckBoxExtensi on : System.Web.UI.W ebControls.Chec kBox
// i also tried like this but it does not work neither:
// public class CheckBoxExtensi on : System.Web.UI.W ebControls.Chec kBox,
IPostBackDataHa ndler
{
public CheckBoxExtensi on()
{
}

protected override void OnCheckedChange d(EventArgs e)
{
//this method compiles
base.OnCheckedC hanged(e);
}

override bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
NameValueCollec tion postCollection)
{
//this does not compile, compiler says: The modifier 'override' is not
valid for this item
return true;
}

override void IPostBackDataHa ndler.RaisePost DataChangedEven t()
{
//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.W ebControls.Chec kBox,
IPostBackDataHa ndler
{

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

bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
NameValueCollec tion postCollection)
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHa ndler. 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 IPostBackDataHa ndler.RaisePost DataChangedEven t()
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHa ndler. 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.LoadPostDa ta neither
base.RaisePostD ataChangedEvent . 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 1436
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******@betti nghouses.com_NO SPAM> wrote in message
news:Op******** *****@TK2MSFTNG P12.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.W ebControls.Chec kBox.
Its full definition is : public class CheckBox : WebControl,
IPostBackDataHa ndler (notice the interface that is implemented)

I want to override 3 methods belonging to that class:

1. protected virtual void OnCheckedChange d(EventArgs e) //Inherited from
the class itself
2. IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
NameValueCollec tion
postCollection) // defined in the IPostBackDataHa ndler interface
3. IPostBackDataHa ndler.RaisePost DataChangedEven t() // defined in the
IPostBackDataHa ndler interface

Methods 2 and 3 are not defined by the class CheckBox class itself but as
the class implements the interface IPostBackDataHa ndler 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.Collecti ons;
using System.Collecti ons.Specialized ;
using System.Web.UI;

namespace Bos.CustomContr ols
{
public class CheckBoxExtensi on : System.Web.UI.W ebControls.Chec kBox
// i also tried like this but it does not work neither:
// public class CheckBoxExtensi on : System.Web.UI.W ebControls.Chec kBox,
IPostBackDataHa ndler
{
public CheckBoxExtensi on()
{
}

protected override void OnCheckedChange d(EventArgs e)
{
//this method compiles
base.OnCheckedC hanged(e);
}

override bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
NameValueCollec tion postCollection)
{
//this does not compile, compiler says: The modifier 'override' is not
valid for this item
return true;
}

override void IPostBackDataHa ndler.RaisePost DataChangedEven t()
{
//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.W ebControls.Chec kBox,
IPostBackDataHa ndler
{

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

bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
NameValueCollec tion postCollection)
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHa ndler. 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 IPostBackDataHa ndler.RaisePost DataChangedEven t()
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHa ndler. 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.LoadPostDa ta neither
base.RaisePostD ataChangedEvent . 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\abstrac t...

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******@betti nghouses.com_NO SPAM> wrote in message
news:Op******** *****@TK2MSFTNG P12.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.W ebControls.Chec kBox.
Its full definition is : public class CheckBox : WebControl,
IPostBackDataHa ndler (notice the interface that is implemented)

I want to override 3 methods belonging to that class:

1. protected virtual void OnCheckedChange d(EventArgs e) //Inherited from
the class itself
2. IPostBackDataHa ndler.LoadPostD ata(string postDataKey, NameValueCollec tion postCollection) // defined in the IPostBackDataHa ndler interface
3. IPostBackDataHa ndler.RaisePost DataChangedEven t() // defined in the
IPostBackDataHa ndler interface

Methods 2 and 3 are not defined by the class CheckBox class itself but as
the class implements the interface IPostBackDataHa ndler 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.Collecti ons;
using System.Collecti ons.Specialized ;
using System.Web.UI;

namespace Bos.CustomContr ols
{
public class CheckBoxExtensi on : System.Web.UI.W ebControls.Chec kBox
// i also tried like this but it does not work neither:
// public class CheckBoxExtensi on : System.Web.UI.W ebControls.Chec kBox,
IPostBackDataHa ndler
{
public CheckBoxExtensi on()
{
}

protected override void OnCheckedChange d(EventArgs e)
{
//this method compiles
base.OnCheckedC hanged(e);
}

override bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
NameValueCollec tion postCollection)
{
//this does not compile, compiler says: The modifier 'override' is not
valid for this item
return true;
}

override void IPostBackDataHa ndler.RaisePost DataChangedEven t()
{
//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.W ebControls.Chec kBox,
IPostBackDataHa ndler
{

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

bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
NameValueCollec tion postCollection)
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHa ndler. 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 IPostBackDataHa ndler.RaisePost DataChangedEven t()
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHa ndler. 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.LoadPostDa ta neither
base.RaisePostD ataChangedEvent . 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.m ethod() 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
4338
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
1
1689
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 derivies class's that print those into console or into a file. template <class T, class S> class Base{ protected: T t;
2
1192
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
1922
by: Jo | last post by:
class A { public: char text_a; A() { *text_a=0; } ~A() {} }; //-----------------------------------------------------------------------------
8
273
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 other words that could fill in the blank though. "Or am I _______ as usual?"
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.