Click here to Skip to main content
Click here to Skip to main content

Abstract classes and methods in C#

, 15 Jun 2006
Rate this:
Please Sign up or sign in to vote.
A Detailed Analysis of Abstract classes and methods in C#

Introduction

A detailed analysis of Abstract classes and methods in C# with some concrete examples.

The keyword abstract can be used with both classes and methods in C# to declare them as abstract.

The classes, which we can't initialize, are known as abstract classes. They provide only partial implementations. But another class can inherit from an abstract class and can create their instances. You need to mark class members that have no implementation with the abstract modifier. ou also need to label a class containing such members as abstract. A class with abstract members cannot be instantiated with the new operator. You can derive both abstract and non-abstract classes from an abstract base class. Interfaces are implicitly abstract.

They cannot be instantiated, and must be implemented by a non-abstract class. Therefore, you cannot mark interfaces and their members as abstract. You may not combine the abstract modifier with the other inheritance modifier, final. You may not combine either of these inheritance modifiers (abstract and final) with the static modifier.

See the below examples,

Example 1

namespace Abstract
{
 /// <span class="code-SummaryComment"><summary>
</span> /// an abstract class with a non-abstract method
 /// <span class="code-SummaryComment"></summary>
</span> 
 abstract class MyAbs
 {
  public void NonAbMethod()
  {
   Console.WriteLine("Non-Abstract Method");
  }
 }

 class MyClass : MyAbs
 {
 }

 class Class1
 {
  /// <span class="code-SummaryComment"><summary>
</span>  /// The main entry point for the application.
  /// <span class="code-SummaryComment"></summary>
</span>  [STAThread]
  static void Main(string[] args)
  {
   //MyAbs mb = new MyAbs();//not possible to create an instance
   MyClass mc = new MyClass();
   mc.NonAbMethod(); // Displays 'Non-Abstract Method'
  }
 }
}
namespace Abstract2
{
 /// <span class="code-SummaryComment"><summary>
</span> /// An abstract method is a method without any method body.
 /// They are implicitly virtual in C#.
 /// <span class="code-SummaryComment"></summary>
</span> 
 abstract class MyAbs
 {
  public void NonAbMethod()
  {
   Console.WriteLine("Non-Abstract Method");
  }
  public abstract void AbMethod(); // An abstract method
 }

 class MyClass : MyAbs//must implement base class abstract methods
 {
  public override void AbMethod()
  {
   Console.WriteLine("Abstarct method");
  } 
 }

 class Class1
 {
  /// <span class="code-SummaryComment"><summary>
</span>  /// The main entry point for the application.
  /// <span class="code-SummaryComment"></summary>
</span>  [STAThread]
  static void Main(string[] args)
  {
   MyClass mc = new MyClass();
   mc.NonAbMethod();
   mc.AbMethod();
  }
 }
}

For more examples, please see the attachment.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Manoj Kumar G (IND)
Web Developer
India India
It is an inherent passion for Analytical applications that inspired me to study Physics at the graduate level. During these 3 years I was trained in systematic analysis of physical data and application of Mathematics and in due course of time this passion got transformed into an abiding interest towards automation. This motivated me to go for a higher level application oriented program in Software Development. During the last 6+ years I have enriched myself considerably well by working on various projects which really fine tuned my technical skills. I look forward to put into productive use the knowledge gained / insights developed and at the same time continue to learn enhancing my caliber and pursue a career of excellence, rooted in professional ethics, in the field of Software Development.
 
I have strong background in C#, ASP.NET, ASP, VB6.0 and SQL server 2000.

Comments and Discussions

 
QuestionSimple one, nice tooo..... Pinmemberabindavis213-Oct-12 21:04 
GeneralMy vote of 2 Pinmemberraj23karthik29-Apr-11 0:27 
GeneralGive credit where credit is due. PinmemberLarantz18-Oct-06 10:33 
Generaloriginally by V.S.Rajesh Pinmemberjo0ls3-Oct-06 17:08 
GeneralBroken Link PinmemberByteGhost16-Jun-06 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 16 Jun 2006
Article Copyright 2006 by Manoj Kumar G (IND)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid