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

Abstract Class Theory

If we were to define all abstract methods in an abstract class, thereby
making that class non-abstract, and then override the heretofore 'abstract'
methods in a derived class, wouldn't that remove the need to have abstract
class types in C#?

Derived classes from abstract base classes must overrided the abstract
method defininition anyway, so why not just provide a complete definition
for the abstract method when defining the containing base class?

--
Thank you kindly,
Dan Sikorsky BAB, BScE, MSC
Nov 16 '05 #1
6 5779

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:ON**************@TK2MSFTNGP11.phx.gbl...
If we were to define all abstract methods in an abstract class, thereby
making that class non-abstract, and then override the heretofore
'abstract'
methods in a derived class, wouldn't that remove the need to have abstract
class types in C#?

No, it would just achieve the same thing in a different way, without certain
benefits.
Derived classes from abstract base classes must overrided the abstract
method defininition anyway, so why not just provide a complete definition
for the abstract method when defining the containing base class?


The first thing this misses is that there is no complete definition to give.
An abstract method should not be one that can be implemented in the base
class, that misses the point. An abstract method is a method which is only
definable in a derived class.

The second is compiler enforcement of required overrides. By marking a
method abstract, you force the deriver to implement the method. A virtual
method has no such enforcement and removing abstract weakens the contract
and relegates required override information to documentation instead of
source code.
Nov 16 '05 #2
The purpose of my posting was to determine just what the benefits are for
inclusion of an abstract class type in the language. We could propose that
each class method have a default definition - if only to return true. Then
there would be no need for abstract methods, and no abstract classes. Again,
the derived class will be overriding, or even overloading, the base class
anyway.

If you don't know the definition of a method until the base class is
inherited by a derived class and the derived class overrides or overloads
it; just supply a default definition by returning true, or false.

What specific clear benefits to having abstract methods are there?

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP15.phx.gbl...

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:ON**************@TK2MSFTNGP11.phx.gbl...
If we were to define all abstract methods in an abstract class, thereby
making that class non-abstract, and then override the heretofore
'abstract'
methods in a derived class, wouldn't that remove the need to have abstract class types in C#?

No, it would just achieve the same thing in a different way, without

certain benefits.
Derived classes from abstract base classes must overrided the abstract
method defininition anyway, so why not just provide a complete definition for the abstract method when defining the containing base class?
The first thing this misses is that there is no complete definition to

give. An abstract method should not be one that can be implemented in the base
class, that misses the point. An abstract method is a method which is only
definable in a derived class.

The second is compiler enforcement of required overrides. By marking a
method abstract, you force the deriver to implement the method. A virtual
method has no such enforcement and removing abstract weakens the contract
and relegates required override information to documentation instead of
source code.

Nov 16 '05 #3
Dan,

With an abstract method (on an abstract class), you force the derived
class to provide an implementation. Granted, I would usually write virtual
methods with a base implementation if it were possible. However, there are
a number of situations where a base implementation makes absolutely no
sense, and having a do-nothing method (in the form of a default virtual
implementation) doesn't add any benefit either.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
The purpose of my posting was to determine just what the benefits are for
inclusion of an abstract class type in the language. We could propose that
each class method have a default definition - if only to return true. Then
there would be no need for abstract methods, and no abstract classes.
Again,
the derived class will be overriding, or even overloading, the base class
anyway.

If you don't know the definition of a method until the base class is
inherited by a derived class and the derived class overrides or overloads
it; just supply a default definition by returning true, or false.

What specific clear benefits to having abstract methods are there?

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP15.phx.gbl...

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:ON**************@TK2MSFTNGP11.phx.gbl...
> If we were to define all abstract methods in an abstract class, thereby
> making that class non-abstract, and then override the heretofore
> 'abstract'
> methods in a derived class, wouldn't that remove the need to have abstract > class types in C#?
>


No, it would just achieve the same thing in a different way, without

certain
benefits.
> Derived classes from abstract base classes must overrided the abstract
> method defininition anyway, so why not just provide a complete definition > for the abstract method when defining the containing base class?


The first thing this misses is that there is no complete definition to

give.
An abstract method should not be one that can be implemented in the base
class, that misses the point. An abstract method is a method which is
only
definable in a derived class.

The second is compiler enforcement of required overrides. By marking a
method abstract, you force the deriver to implement the method. A virtual
method has no such enforcement and removing abstract weakens the contract
and relegates required override information to documentation instead of
source code.


Nov 16 '05 #4
Dan,
In addition to the other comments.
We could propose that
each class method have a default definition - if only to return true. Abstract methods state there is *NO* default definition possible. Period.

In other words returning true or false is totally inappropriate! As you may
be introducing unexpected or undesired runtime behavior.

Also an Abstract class states that you cannot create an instance of the
class. Period.

The closest you can come to "correctly" implementing an abstract method in
an abstractless language would be for those methods to all raise
NotImplementedExceptions or NotSupportedExceptions. Unfortunately raising
the exception causes possibly hard to find runtime errors, where as the
abstract keyword clearly indicates a compile time error when the derived
class does not implement the method.

You can declare the constructors protected, to insure that no one created an
instance of the class, unfortunately the class itself could create an
instance of itself. Again you will get a compile error if any class tried to
instantiate an abstract class.

Hope this helps
Jay

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl... The purpose of my posting was to determine just what the benefits are for
inclusion of an abstract class type in the language. We could propose that
each class method have a default definition - if only to return true. Then
there would be no need for abstract methods, and no abstract classes.
Again,
the derived class will be overriding, or even overloading, the base class
anyway.

If you don't know the definition of a method until the base class is
inherited by a derived class and the derived class overrides or overloads
it; just supply a default definition by returning true, or false.

What specific clear benefits to having abstract methods are there?

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP15.phx.gbl...

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:ON**************@TK2MSFTNGP11.phx.gbl...
> If we were to define all abstract methods in an abstract class, thereby
> making that class non-abstract, and then override the heretofore
> 'abstract'
> methods in a derived class, wouldn't that remove the need to have abstract > class types in C#?
>


No, it would just achieve the same thing in a different way, without

certain
benefits.
> Derived classes from abstract base classes must overrided the abstract
> method defininition anyway, so why not just provide a complete definition > for the abstract method when defining the containing base class?


The first thing this misses is that there is no complete definition to

give.
An abstract method should not be one that can be implemented in the base
class, that misses the point. An abstract method is a method which is
only
definable in a derived class.

The second is compiler enforcement of required overrides. By marking a
method abstract, you force the deriver to implement the method. A virtual
method has no such enforcement and removing abstract weakens the contract
and relegates required override information to documentation instead of
source code.


Nov 16 '05 #5

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:iTshd.42920$R05.11994@attbi_s53...

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
The purpose of my posting was to determine just what the benefits are for
inclusion of an abstract class type in the language. We could propose
that
each class method have a default definition - if only to return true.
Then
there would be no need for abstract methods, and no abstract classes.

Again,
the derived class will be overriding, or even overloading, the base class
anyway.

If you don't know the definition of a method until the base class is
inherited by a derived class and the derived class overrides or overloads
it; just supply a default definition by returning true, or false.

What specific clear benefits to having abstract methods are there?


If you read into the Patterns literature, you can see that the requirement
to define an interface presents different benefits and costs than the
option
of defining a base class. In many patterns, the creation of code in the
base class would defeat the pattern, or change the pattern to another,
with
different attributes. Sometimes, the ONLY right answer is one in which
the
abstract class is defined as purely abstract.

In addition, if you define two abstract classes, you can have a concrete
class inherit from both. On the other hand, you cannot have a concrete
class inherit from two base classes due to C#'s limitation against
multiple
inheritance. Therefore, with the restriction against multiple
inheritance,
the use of abstract classes is absolutely essential to the ability of a
developer to specify multiple interfaces for an object.


Actually you are confusing abstract classes and interfaces.
A C# abstract class is a literal class and only one abstract class can be
inherited from just as any other base class acts. An interface, on the other
hand, I think more closely matches waht you are tying to get at here, where
any number of interfaces may be implemented by a given class.

Nov 16 '05 #6

"Dan Sikorsky" <ds****@gte.net> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
The purpose of my posting was to determine just what the benefits are for
inclusion of an abstract class type in the language. We could propose that
each class method have a default definition - if only to return true. Then
there would be no need for abstract methods, and no abstract classes.
Again,
the derived class will be overriding, or even overloading, the base class
anyway.


You could, but is there any clear benefit in that? All that is different is
that instead of an abstract modifier you get a form of expectation or
documentation that declares return type standards(since bool is not the only
return type). There is no complexity shift and abstract is arguably the
simpler pattern.

Also abstract forces implementation, as has been pointed out. A virtual
method is optional, an abstract is required.
Nov 16 '05 #7

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

Similar topics

18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
7
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
2
by: Dan Holmes | last post by:
Suppose i have this class declaration: public abstract class ConfigurableComponent : Component, IConfigure if IConfigure has a method with this signature: ...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
4
by: Arne Schmitz | last post by:
If i have an abstract base class, that only contains pure virtual methods (and maybe some non-virtual methods), is a vtable still being generated, for the first derived class that implements those...
1
by: vineelaNali | last post by:
hello, Please give one real time example in using abstract class in real time give me one example explaning the theory part .
21
by: Mr.SpOOn | last post by:
Hi, I'm going to work on a project to represent some musical theory in Python, in an object oriented way. I have to manage many elements of music such as notes, intervals, scales, chords and so...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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,...
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
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...

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.