Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# allow for an abstract class with no abstract members?

Tags:

The C# spec, section 10.1.1.1, states:

An abstract class is permitted (but not required) to contain abstract members.

This allows me to create classes like this:

public abstract class A {     public void Main()      {         // it's full of logic!     } } 

Or even better:

public abstract class A {     public virtual void Main() { } }  public abstract class B : A {     public override sealed void Main()     {         // it's full of logic!     } } 

This is really a concrete class; it's only abstract in so far as one can't instantiate it. For example, if I wanted to execute the logic in B.Main() I would have to first get an instance of B, which is impossible.

If inheritors don't actually have to provide implementation, then why call it abstract?

Put another way, why does C# allow an abstract class with only concrete members?

I should mention that I am already familiar with the intended functionality of abstract types and members.

like image 551
Justin R. Avatar asked Jun 08 '10 18:06

Justin R.


People also ask

Why do we use %d in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Why semicolon is used in C?

Role of Semicolon in C: Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.

Why C language is called C?

After language 'B', Dennis Ritchie came up with another language which was based upon 'B'. As in alphabets B is followed by C and hence he called this language as 'C'.

What do we use C for?

C is a programming language that is both versatile and popular, allowing it to be used in a vast array of applications and technologies. It can, for example, be used to write the code for operating systems, complex programs and applications, and everything in between.


1 Answers

Perhaps a good example is a common base class that provides shared properties and perhaps other members for derived classes, but does not represent a concrete object. For example:

public abstract class Pet {     public string Name{get;set;} }  public class Dog : Pet {     public void Bark(){ ... } } 

All pets have names, but a pet itself is an abstract concept. An instance of a pet must be a dog or some other kind of animal.

The difference here is that instead of providing a method that should be overridden by implementors, the base class declares that all pets are composed of at least a Name property.

like image 68
Michael Petito Avatar answered Oct 18 '22 05:10

Michael Petito