473,394 Members | 1,697 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,394 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 19 '05 #1
1 1391
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.

--
Matt Berther
http://www.mattberther.com
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 19 '05 #2

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

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
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...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
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) {
1
by: Tony Johansson | last post by:
Hello! I have this small class CustomException with one ctor see below. In this class I have both override the ToString and Message. Now to my question if I made this call string ss =...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
5
by: The Cool Giraffe | last post by:
Suppose there's an abstract base class and two inheriting classes declared as follows. class Shape {double sizeA;}; class Ellipse : Shape {double sizeB}; class Rect : Shape {double sizeC}; ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.