By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,594 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

Abstract Class Theory

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.