I have a base class and a chain of derived classes.
The base and its derived classes are all abstract except the last in chain
(a concrete class).
I want to provide some functionalities in the base class by defining a
member as e.g. "protected virtual MyReturnClass MyMethod(){...} "
The base classes, including the last (concrete class) adds on this
functionality by defining members as e.g. "protected override MyReturnClass
MyMethod(){... return base.MyMethod() ;}"
This works fine, however I often forget to implement MyMethod in the classes
(both abstract and the concrete class(es), except (obviously) the "top
base").
Of course this is found during testing, but I would like the compiler to, at
least warn me that "my intention is not fulfilled" (better yet with an
error).
Abstract member declarations would enforce this, but abstract members cannot
contain any body, so I am looking for something like a special abstract
modifier which accepts code-bodies (I know this does not exist).
Is there a way to force overriding by combining some of the modifiers or by
other means (e.g. interfaces etc.) ?
What if the "chained functionalites" needs to be bodies of constructors (as
it is in my actual case) ? 3 4307
Magne Ryholt <ma**********@b luezone.no> wrote: I have a base class and a chain of derived classes. The base and its derived classes are all abstract except the last in chain (a concrete class). I want to provide some functionalities in the base class by defining a member as e.g. "protected virtual MyReturnClass MyMethod(){...} " The base classes, including the last (concrete class) adds on this functionality by defining members as e.g. "protected override MyReturnClass MyMethod(){... return base.MyMethod() ;}"
This works fine, however I often forget to implement MyMethod in the classes (both abstract and the concrete class(es), except (obviously) the "top base"). Of course this is found during testing, but I would like the compiler to, at least warn me that "my intention is not fulfilled" (better yet with an error). Abstract member declarations would enforce this, but abstract members cannot contain any body, so I am looking for something like a special abstract modifier which accepts code-bodies (I know this does not exist).
Is there a way to force overriding by combining some of the modifiers or by other means (e.g. interfaces etc.) ? What if the "chained functionalites" needs to be bodies of constructors (as it is in my actual case) ?
Define an abstract member and provide a similarly named concrete
member, eg DoSomething and DoSomethingBase Impl. Your derived classes
would then call DoSomethingBase Impl in their implementations of
DoSomething.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<snip> Define an abstract member and provide a similarly named concrete member, eg DoSomething and DoSomethingBase Impl. Your derived classes would then call DoSomethingBase Impl in their implementations of DoSomething.
A slight variation of Jon's suggestion would be to have a concrete,
non-virtual method in the abstract class that calls a protected, abstract
method in the abstract class. Something like:
// in the abstract class
...
public void DoSomething()
{
...
AfterDoSomethin g();
...
}
protected abstract void AfterDoSomethin g();
...
// in the concrete class
...
protected override void AfterDoSomethin g()
{
...
}
The advantage to doing this is that it does not require the inheriting class
to call the base class method.
Regards,
Daniel
Thanks to Jon and Daniel
Do you know some short and good explanation (or a url) to why abstract
methods should not be allowed to have implementation in the class it is
declared ?
I have certainly had the need to implement and enforce derived class
overriding (but because it is not possible I have not seen potential
problems with it)
Would it break "good OO practice etc. ? polymorphism ?
Perhaps the word abstract would not be the correct one anymore (I fully
understand the meaning of abstract regarding classes, regarding methods or
properties the word is a bit more "fuzzy" to me, but my mother-tongue is not
english :).
I have seen that C# 2.0 introduces a new keyword, namely partial, perhaps
this word would be better ? (C# includes different interpretations of the
same keyword anyway e.g. new)
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Magne Ryholt <ma**********@b luezone.no> wrote: I have a base class and a chain of derived classes. The base and its derived classes are all abstract except the last in chain (a concrete class). I want to provide some functionalities in the base class by defining a member as e.g. "protected virtual MyReturnClass MyMethod(){...} " The base classes, including the last (concrete class) adds on this functionality by defining members as e.g. "protected override MyReturnClass MyMethod(){... return base.MyMethod() ;}"
This works fine, however I often forget to implement MyMethod in the classes (both abstract and the concrete class(es), except (obviously) the "top base"). Of course this is found during testing, but I would like the compiler to, at least warn me that "my intention is not fulfilled" (better yet with an error). Abstract member declarations would enforce this, but abstract members cannot contain any body, so I am looking for something like a special abstract modifier which accepts code-bodies (I know this does not exist).
Is there a way to force overriding by combining some of the modifiers or by other means (e.g. interfaces etc.) ? What if the "chained functionalites" needs to be bodies of constructors (as it is in my actual case) ?
Define an abstract member and provide a similarly named concrete member, eg DoSomething and DoSomethingBase Impl. Your derived classes would then call DoSomethingBase Impl in their implementations of DoSomething.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Merlin |
last post by:
Hi
Been reading the GOF book and started to make the distinction between
Class and Interface inheritance. One question though:
Do pure abstract classes have representations? (data members?)
What about abstract classes?
Should abstract classes have a destructor and or constructor? What
about pure abstract classes?
|
by: santosh |
last post by:
Hi All ,
Why does the below code doesn't compile??
class Interface
{
public:
virtual void funA() = 0;
virtual void funB() = 0;
virtual void funD() = 0;
Interface();
|
by: Tony Johansson |
last post by:
Hello!
Assume you have an abstract class called Body and a derived class called
cylinder.
When you have an abstract class you can't instansiate an object.
As you can see in the abstract class there are one data member called
density that you can't access because there will not be any object of class
Body. The data member density will be inheritated and all the member
functions.
So when you have an instance of class Cylinder and you...
|
by: Brett |
last post by:
I'm still trying to figure out concrete reasons to use one over the other.
I understand the abstract class can have implementation in its methods and
derived classes can only inherit one abstract class. The interface has
implied abstract methods/properties and derived classes can inherit multiple
interfaces. The interface properties/methods have no implementation.
Besides definitions of the two, what are some conceptual reasons to use...
|
by: WithPit |
last post by:
I am trying to create an managed wrapper but have some problems with it by
using abstract classes.
In my unmanaged library code i had the following three classes with the
following hierarchy
Referenced (class)
Object (abstract class, inheriting from referenced)
Node (class, inheriting from object)
| |
by: Fergal Moran |
last post by:
Hello Guys
I'm a C++ programmer moving to C# and I'm trying to do something which
I regularly do in C++ and am wondering why it doesn't work in C#.
Basically in C++ I can do something similar to the following
class A{
public:
virtual void foo()=0;
|
by: tron.thomas |
last post by:
Please consider the following code:
class Abstract
{
public:
virtual ~Abstract() {}
virtual void Method() = 0;
};
class Concrete : public virtual Abstract
|
by: J.M. |
last post by:
I have a question concerning inheritance: can an abstract (parent) class
have an abstract object? I would like to make a concrete child inherit from
this class by inheriting from this object. Let me try to make this clear by
example: The following should be "abstract".
"class motor" is abstract because it has a virtual member
"virtual int calculate_horsepower() = 0"
"class car" should have
"motor M;"
|
by: Jess |
last post by:
Hello,
If I have a class that has virtual but non-pure declarations, like
class A{
virtual void f();
};
Then is A still an abstract class? Do I have to have "virtual void
f() = 0;" instead? I think declaring a function as "=0" is the same
|
by: Miguel Guedes |
last post by:
Hello,
I recently read an interview with Bjarne Stroustrup in which he says
that pure abstract classes should *not* contain any data. However, I
have found that at times situations are when it would be useful to have
/some/ data defined in such an abstract class for reasons of forming a
common block of data existing in or used by all descendant classes (see
example below.) In such a case where a common block of data *always*
exists,...
|
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...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |