473,237 Members | 1,245 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,237 software developers and data experts.

Need help: about OOP inheritance/abstract class

Tee
Hi,

I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I want
to force all the usercontrol that inheriting this base usercontrol to
override the method with its own code. How do I do it?

I have tried to make the base usercontrol an abstract class (mustinherits +
mustoverrides), this works, but all the usercontrols that inherit it will
not able to open in VS designer because of inherting an abstract class.

I have also tried to use interface, this works as well, but I can't force
those usercontrols that inherit base usercontrol MUST have the interface.

Anyone has a solution for this?
Thanks,
Tee
Jul 21 '05 #1
15 2355
Tee wrote:
Hi,

I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I
want to force all the usercontrol that inheriting this base usercontrol to
override the method with its own code. How do I do it?


If you are going to /force/ them to override it ( polymorphism ) why have it
in the first place?

You should define an *interface* with a stub method and force them to
implement it.

I do this in one of my web service applications. There are two interfaces
and a base abstract class. The interface accounts for methods that must
be implemented in the derived classes, but which have no default method in
the abstract class. So each derived class has one abstract class and two
interfaces to inherit from.
--
http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com

Jul 21 '05 #2
I haven't tried, but have you tried to use inheirtance AND interface
together?

i.e.
* base class with stuff that should be inherited as-is
* interface with the definition of the method that the inheriting
classes should implement/override
* inheriting/implementing classes: they inherit from the base class,
AND they implement the interface.

The children should then get all that the base class provides and Be
Forced to implement the method(s) defined in the interface.
HTH,
F.O.R.

Jul 21 '05 #3
Tee
Hi,
You should define an *interface* with a stub method and force them to implement it.
Is stub method a keyword?
I do this in one of my web service applications. There are two ............
What I am doing is a usercontrol, what I meant "I can't open the usercontrol
in VS designer" is because when it opens up the form/usercontrol, it
actually called it as "new", and abstract class can't be declared as new"

Thanks.
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:RZ*****************@newsread3.news.pas.earthl ink.net... Tee wrote:
Hi,

I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I
want to force all the usercontrol that inheriting this base usercontrol to override the method with its own code. How do I do it?
If you are going to /force/ them to override it ( polymorphism ) why have

it in the first place?

You should define an *interface* with a stub method and force them to
implement it.

I do this in one of my web service applications. There are two interfaces
and a base abstract class. The interface accounts for methods that must
be implemented in the derived classes, but which have no default method in
the abstract class. So each derived class has one abstract class and two
interfaces to inherit from.
--
http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com

Jul 21 '05 #4
Tee
I have tried it.
The children should then get all that the base class provides and Be
Forced to implement the method(s) defined in the interface.
HTH,
F.O.R.
The base class that implements the interface will automatically has the
method in the class (just an empty method, no code).
And yes, the children get all the base class provided, and they inherit the
empty method as well ... the problem is it won't force the children to
overrides it.
Thanks.
"Olorin" <fr*************@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com... I haven't tried, but have you tried to use inheirtance AND interface
together?

i.e.
* base class with stuff that should be inherited as-is
* interface with the definition of the method that the inheriting
classes should implement/override
* inheriting/implementing classes: they inherit from the base class,
AND they implement the interface.

The children should then get all that the base class provides and Be
Forced to implement the method(s) defined in the interface.
HTH,
F.O.R.

Jul 21 '05 #5
This is not just a problem with user controls; it is also a problem
with inheriting Forms.

You cannot use an abstract base class for the reasons you mentioned.
There is no way around it at present.

The best you can do is write the methods in the base class to throw a
NotImplementedException. Then if the child class does not override them
it will blow up at run time. There is no way to enforce the override
via a compile-time check.

Jul 21 '05 #6
Tee wrote:

Is stub method a keyword?
Sorry, bad choice of words.

Have you ever used the /interface/ keyword though?

You define methods, but just in terms of the input parameters.

Then, its up to the inheriting classes to implement. Similar to, but not
exactly, to an abstract class.

And there's no *default* implementation.

What I am doing is a usercontrol, what I meant "I can't open the
usercontrol in VS designer" is because when it opens up the
form/usercontrol, it actually called it as "new", and abstract class can't
be declared as new"
No, but you can create a class that instantiates the user control in the
constructor, or that inherits it, and inherit the interfaces.

Thanks.
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:RZ*****************@newsread3.news.pas.earthl ink.net...
Tee wrote:
> Hi,
>
> I have a base usercontrol with a method (blank method, no code), I have
> another few usercontrols that will inherit this base usercontrol, but I
> want to force all the usercontrol that inheriting this base usercontrol to > override the method with its own code. How do I do it?


If you are going to /force/ them to override it ( polymorphism ) why have

it
in the first place?

You should define an *interface* with a stub method and force them to
implement it.

I do this in one of my web service applications. There are two
interfaces
and a base abstract class. The interface accounts for methods that must
be implemented in the derived classes, but which have no default method
in
the abstract class. So each derived class has one abstract class and
two interfaces to inherit from.
--
http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com


--
http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com

Jul 21 '05 #7
Why not just(shortened):

abstract class Abstract
{
public abstract void MustOverrideThisAbstractMethod ( ) ;
}

class Concrete : Abstract
{
override public void MustOverrideThisAbstractMethod ( )
{
//Implement something here.
}

}

Declaring a method as abstract within an abstract class means the base class
will demand an implementation of that method from the concrete derived
class.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Tee" <th*@streamyx.com> wrote in message
news:un*************@TK2MSFTNGP10.phx.gbl...
Hi,

I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I
want
to force all the usercontrol that inheriting this base usercontrol to
override the method with its own code. How do I do it?

I have tried to make the base usercontrol an abstract class (mustinherits
+
mustoverrides), this works, but all the usercontrols that inherit it will
not able to open in VS designer because of inherting an abstract class.

I have also tried to use interface, this works as well, but I can't force
those usercontrols that inherit base usercontrol MUST have the interface.

Anyone has a solution for this?
Thanks,
Tee

Jul 21 '05 #8
Tee
Hi Dennis,

See my previous post:
I have tried to make the base usercontrol an abstract class (mustinherits + mustoverrides), this works, but all the usercontrols that inherit it will
not able to open in VS designer because of inherting an abstract class.

This just won't work on forms or usercontrols ... it won't effect runtime,
but it's trouble enough if you can't open it in VS Designer.
Thanks,
Tee
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:dN*******************@news4.e.nsc.no... Why not just(shortened):

abstract class Abstract
{
public abstract void MustOverrideThisAbstractMethod ( ) ;
}

class Concrete : Abstract
{
override public void MustOverrideThisAbstractMethod ( )
{
//Implement something here.
}

}

Declaring a method as abstract within an abstract class means the base class will demand an implementation of that method from the concrete derived
class.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Tee" <th*@streamyx.com> wrote in message
news:un*************@TK2MSFTNGP10.phx.gbl...
Hi,

I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I
want
to force all the usercontrol that inheriting this base usercontrol to
override the method with its own code. How do I do it?

I have tried to make the base usercontrol an abstract class (mustinherits +
mustoverrides), this works, but all the usercontrols that inherit it will not able to open in VS designer because of inherting an abstract class.

I have also tried to use interface, this works as well, but I can't force those usercontrols that inherit base usercontrol MUST have the interface.
Anyone has a solution for this?
Thanks,
Tee


Jul 21 '05 #9
Sorry, i should probably read the posts fully before replying them.

I had a bunch of webforms once which all needed common extra features in
order
to provide the functionality of a step in a wizard.
There was no way i could force those webforms to implement my custom
interface
in addition to inherit System.Web.UI.Page.
But i myself though, knew which ones of them should provide that
functionality,
and had them implement that interface.
class WizardStep1 : System.Web.UI.Page, IWizardStep

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Tee" <th*@streamyx.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Dennis,

See my previous post:
I have tried to make the base usercontrol an abstract class (mustinherits
+

mustoverrides), this works, but all the usercontrols that inherit it will
not able to open in VS designer because of inherting an abstract class.

This just won't work on forms or usercontrols ... it won't effect runtime,
but it's trouble enough if you can't open it in VS Designer.
Thanks,
Tee
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:dN*******************@news4.e.nsc.no...
Why not just(shortened):

abstract class Abstract
{
public abstract void MustOverrideThisAbstractMethod ( ) ;
}

class Concrete : Abstract
{
override public void MustOverrideThisAbstractMethod ( )
{
//Implement something here.
}

}

Declaring a method as abstract within an abstract class means the base

class
will demand an implementation of that method from the concrete derived
class.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Tee" <th*@streamyx.com> wrote in message
news:un*************@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> I have a base usercontrol with a method (blank method, no code), I have
> another few usercontrols that will inherit this base usercontrol, but I
> want
> to force all the usercontrol that inheriting this base usercontrol to
> override the method with its own code. How do I do it?
>
> I have tried to make the base usercontrol an abstract class (mustinherits > +
> mustoverrides), this works, but all the usercontrols that inherit it will > not able to open in VS designer because of inherting an abstract class.
>
> I have also tried to use interface, this works as well, but I can't force > those usercontrols that inherit base usercontrol MUST have the interface. >
> Anyone has a solution for this?
>
>
> Thanks,
> Tee
>
>



Jul 21 '05 #10
Tee
> Have you ever used the /interface/ keyword though?
Yes, I know what is it and used it before. Since I can't force a class to
implements an interface, I don't think this is what I am looking for.
No, but you can create a class that instantiates the user control in the
constructor, or that inherits it, and inherit the interfaces. Sorry I am somehow a bit lost in this, I can't figure it out. Can you tell
me how to instantiates user control in constructor?
I think this should be what I need, greatly appreciate if you could
elaborate on this.
Thanks,
Tee

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:uP*****************@newsread3.news.pas.earthl ink.net... Tee wrote:

Is stub method a keyword?


Sorry, bad choice of words.

Have you ever used the /interface/ keyword though?

You define methods, but just in terms of the input parameters.

Then, its up to the inheriting classes to implement. Similar to, but not
exactly, to an abstract class.

And there's no *default* implementation.

What I am doing is a usercontrol, what I meant "I can't open the
usercontrol in VS designer" is because when it opens up the
form/usercontrol, it actually called it as "new", and abstract class can't be declared as new"


No, but you can create a class that instantiates the user control in the
constructor, or that inherits it, and inherit the interfaces.

Thanks.
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:RZ*****************@newsread3.news.pas.earthl ink.net...
Tee wrote:

> Hi,
>
> I have a base usercontrol with a method (blank method, no code), I have > another few usercontrols that will inherit this base usercontrol, but I > want to force all the usercontrol that inheriting this base usercontrol
to
> override the method with its own code. How do I do it?

If you are going to /force/ them to override it ( polymorphism ) why
have it
in the first place?

You should define an *interface* with a stub method and force them to
implement it.

I do this in one of my web service applications. There are two
interfaces
and a base abstract class. The interface accounts for methods that

must be implemented in the derived classes, but which have no default method
in
the abstract class. So each derived class has one abstract class and
two interfaces to inherit from.
--
http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com


--
http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com

Jul 21 '05 #11

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
This is not just a problem with user controls; it is also a problem
with inheriting Forms.

You cannot use an abstract base class for the reasons you mentioned.
There is no way around it at present.

The best you can do is write the methods in the base class to throw a
NotImplementedException. Then if the child class does not override them
it will blow up at run time. There is no way to enforce the override
via a compile-time check.

Well there is one workaround which I've used. You can use use compile time
constants to declare your class such that the MustInherit attribute is only
enforced in Release builds e.g.

#If Debug then
Public MyClass
#Else
Public MustInherit MyClass
#End if
....

Any methods that should be overridden can be given a default implementation
in the debug build (to write a message to the console or throw an exception)
e.g.

....
#If Debug then
Public Sub MethodThatMustBeOverriden()
Debug.Fail("Method not overridden")
End Sub
#Else
Public MustOverride Sub MethodThatMustBeOverriden()
#End if

This has worked OK for me in the past.

Hope this helps,

Nick Hall


Jul 21 '05 #12
> > The children should then get all that the base class provides and
Be
Forced to implement the method(s) defined in the interface.
HTH,
F.O.R.
The base class that implements the interface will automatically has

the method in the class (just an empty method, no code).
And yes, the children get all the base class provided, and they inherit the empty method as well ... the problem is it won't force the children to overrides it.


I didn't say that the base class should implement the interface ;-)
ANd, yes I now realize that the solution I proposed would have the draw
back of the
base class not implementing the method(s) in your interface. SO it
might or might not work for you.

HTH,
F.O.R.

Jul 21 '05 #13
Very clever. Perhaps I'll try that on my projects next time.

Jul 21 '05 #14
Tee
Very great idea, thanks!
"Nick Hall" <ni***@aslan.nospam.co.uk> wrote in message
news:un**************@TK2MSFTNGP15.phx.gbl...

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
This is not just a problem with user controls; it is also a problem
with inheriting Forms.

You cannot use an abstract base class for the reasons you mentioned.
There is no way around it at present.

The best you can do is write the methods in the base class to throw a
NotImplementedException. Then if the child class does not override them
it will blow up at run time. There is no way to enforce the override
via a compile-time check.
Well there is one workaround which I've used. You can use use compile

time constants to declare your class such that the MustInherit attribute is only enforced in Release builds e.g.

#If Debug then
Public MyClass
#Else
Public MustInherit MyClass
#End if
...

Any methods that should be overridden can be given a default implementation in the debug build (to write a message to the console or throw an exception) e.g.

...
#If Debug then
Public Sub MethodThatMustBeOverriden()
Debug.Fail("Method not overridden")
End Sub
#Else
Public MustOverride Sub MethodThatMustBeOverriden()
#End if

This has worked OK for me in the past.

Hope this helps,

Nick Hall

Jul 21 '05 #15
Tee wrote:
Have you ever used the /interface/ keyword though? Yes, I know what is it and used it before. Since I can't force a class to
implements an interface, I don't think this is what I am looking for.
No, but you can create a class that instantiates the user control in the
constructor, or that inherits it, and inherit the interfaces.

Sorry I am somehow a bit lost in this, I can't figure it out. Can you tell
me how to instantiates user control in constructor?


I used this trick when I was trying to get an XmlDocument to load at runtime
(static), by instantiating it. Normally, I had to run the .Load method --
but I can't run a method when I declare, I have to declare, then .Load.

So, I created a new class with a constructor that did the load.

In my calling class

CustomXmlDocClass myXml = new CustomXmlDocClass()

Then in the constructor of the instantiated class:

CustomXmlDocClass
{

public XmlDocument xd;

CustomXmlDocCalss()
//the constructor
{
XmlDocument _xd = new XmlDocument();
_xd.Load("whatever.xml");
xd = _xd;
}

}

Now myXml is loaded with whatever.xml at run time.
I think this should be what I need, greatly appreciate if you could
elaborate on this.
Thanks,
Tee

"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:uP*****************@newsread3.news.pas.earthl ink.net...
Tee wrote:

> Is stub method a keyword?


Sorry, bad choice of words.

Have you ever used the /interface/ keyword though?

You define methods, but just in terms of the input parameters.

Then, its up to the inheriting classes to implement. Similar to, but
not exactly, to an abstract class.

And there's no *default* implementation.

> What I am doing is a usercontrol, what I meant "I can't open the
> usercontrol in VS designer" is because when it opens up the
> form/usercontrol, it actually called it as "new", and abstract class can't > be declared as new"


No, but you can create a class that instantiates the user control in the
constructor, or that inherits it, and inherit the interfaces.
>
> Thanks.
>
>
> "Elementary Penguin" <si*****@hare.krishna> wrote in message
> news:RZ*****************@newsread3.news.pas.earthl ink.net...
>> Tee wrote:
>>
>> > Hi,
>> >
>> > I have a base usercontrol with a method (blank method, no code), I have >> > another few usercontrols that will inherit this base usercontrol,
>> > but I >> > want to force all the usercontrol that inheriting this base usercontrol > to
>> > override the method with its own code. How do I do it?
>>
>> If you are going to /force/ them to override it ( polymorphism ) why have > it
>> in the first place?
>>
>> You should define an *interface* with a stub method and force them to
>> implement it.
>>
>> I do this in one of my web service applications. There are two
>> interfaces
>> and a base abstract class. The interface accounts for methods that must >> be implemented in the derived classes, but which have no default
>> method in
>> the abstract class. So each derived class has one abstract class and
>> two interfaces to inherit from.
>>
>>
>> --
>> http://texeme.com
>> Textcasting Technology
>> Incognito Blog
>> http://incognito.texeme.com
>>


--
http://texeme.com
Textcasting Technology
Incognito Blog
http://incognito.texeme.com


--
Texeme
http://texeme.com

Jul 21 '05 #16

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

Similar topics

13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
2
by: Bonj | last post by:
Hello Can anyone assist with the following class hierarcy problem? I have a series of window classes, the object model currently being as such: Window / | \ / | \...
11
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
6
by: Nak | last post by:
Hi there, I'm having issues inheriting from a base form of mine. The base form contains no user controls but quite a few properties, events and overridable methods. When attempting to...
7
by: shintu | last post by:
Hi, For the following code snippet, the compiler complains for "mi *mi1=new mi;" statement //******************************************************* #include <iostream> using namespace std;...
9
by: relient | last post by:
I am trying to understand the code below but I need some affirmation of the following first: The reason I inherit the "Base" class method implementation instead of the "Derived" class method in the...
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: superheathen | last post by:
Hi, While I think I have a reasonable grasp of C++ in general, I've never really used it more than a convenient C. as such, I'm finding very basic OO confusing, not so much the concept, but how...
2
by: mmcgarry.work | last post by:
Hi, I would like to follow Stroustrup's advice of separating an object interface (abstract class) from an object implementation (concrete class), See Section 15.2.5 in Stroustrup 3rd Edition. ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.