|
-
December 11th, 2008, 03:49 PM
#10
Re: abstract class
When you declare a method as abstract AND provide an implementation, then:
1) The derviced class must over-ride it with an implementation.
2) The derived class may call the base class method internally.
Now we look at class hierarchies. public inheritance means "is-a". Consider:
Code:
class Foo()
{
}
class Bar() : public Foo()
{
}
Now you write a whole bunch of business requirements and other documents about "Foo". You MUST be able to do a "search/replace" of the word "Foo" with "Bar" (and make no other changes) and still have the document be 100% correct, otherwise public inheritcance is NOT appropriate.
Second, it is generally a good idea to make all non-leaf (base) classes abstract. This can be accomplished by the destructor being virtual allone, but it is often has advantages to make the people who will use your base class THINK about key operations, even if 99% of the derived classes all have the same behaviour. This is exactly where an abstract method with implementation applies.
FYI: C# does not support abstract with implementation. You can come close with an abstract method and a protected method (slightly different names). The derived classes can then call the protected method in the base. This pattern is also used in C++ by a fair number of software development shops.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|