I.T. Discussion Community!
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#OOP   Print This     
  From the October 2015 Issue of Prestwood eMag
 
C# OOP:
C# Abstraction (abstract, override)
 
Posted 11 years ago on 12/23/2008 and updated 1/28/2009
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page
 Tags: C# , Abstraction

KB101344



General Info: Abstract Class / Abstract Member

An abstract class member is a member that is specified in a class but not implemented. Classes that inherit from the class will have to implement the abstract member. Abstract members are a technique for ensuring a common interface with descendant classes. An abstract class is a class you cannot instantiate. A pure abstract class is a class with only abstract members.

Languages Focus: Abstraction

Abstraction is supported at various levels with each language. A language could enforce abstraction at the class level (either enforcing a no-instantiation rule or a only abstract members rule), and with class members (member methods and/or properties).

C# Abstraction

C# supports abstract class members and abstract classes using the abstract modifier.

An abstract class is a class with one or more abstract members and you cannot instantiate an abstract class. However, you can have additional implemented methods and properties.

An abstract member is either a method (implicitly virtual), property, indexer, or event in an abstract class. You can add abstract members ONLY to abstract classes using the abstract keyword. Then you override it in a descendant class with Override.

Syntax Example:
abstract public class Cyborg : System.Object
{
  abstract public void Speak(string pMessage);
}
public class Series600 : Cyborg
{
  public override void Speak(string pMessage)  
  {
    MessageBox.Show(pMessage);  
  }
}

An Abstract Example

The following demonstrates the abstract method above. The following form code assumes a form with a button.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CR_Abstraction
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Series600 MyKiller = new Series600();
      MyKiller.Speak("I'll be back.");
      MyKiller.Greet();

    }
  }
 
  abstract public class Cyborg : System.Object
  {
    abstract public void Speak(string pMessage);
    abstract public void Walk();
    public void Greet()
    {
      MessageBox.Show("Hello");
    }
  }
 
  public class Series600 : Cyborg
  {
    public override void Speak(string pMessage)
    {
      MessageBox.Show(pMessage);
    }

    public override void Walk()
    {
         //Implement walk here.
    }
}
}

Abstract Properties

C# does support setting a property to abstract with the abstract keyword. A single property line with the abstract keyword along with indicating empty get; and set; methods is all that's required. In most cases, you'll probably also want to define the property's supporting member field as protected for visibility in descendant classes.

For example, we could enhance our classes above as follows:

public abstract class Cyborg : System.Object
{
protected string cyborgName; //Suggested member field

public abstract string CyborgName {get; set;} //Abstract property.

  public abstract void Speak(string pMessage);
  public abstract void Walk();

public virtual void Greet()
  {
    MessageBox.Show("Hello human");
  }
}

public class Series600 : Cyborg
{
public override string CyborgName
{
get
{
return cyborgName;
}
set
{
cyborgName = value;
}
}
 
public override void Speak(string pMessage)
{
MessageBox.Show(pMessage);
}
 
public override void Walk()
{
//Implement walk here.
}

  public override void Greet()
{
MessageBox.Show("Hello, I am " + CyborgName + ".");
}
}

 

Now you can use our newly modified class as follows:

Series600 MyKiller = new Series600();
 
MyKiller.CyborgName = "John";
MyKiller.Greet();
MyKiller.Speak("I'll be back.");

First Class Requirement

Visual Studio requires that designers use the first class in the file. Notice above that our Cyborg and Series600 classes come after the Form1 class. This is not normally an issue because classes are usually put in their own class file. For demos, I like to put the classes in a simple project with a single form with a single button on it that exercises the exercise.

Summary

Abstraction is an important aspect of your software design and C# implements a robust set of abstract features. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.

More Info

Definition:  Abstract Class / Abstract Member

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1235A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

1 Intermediate Level Question

Question #1: True or False?

You can add abstract members ONLY to abstract classes using the abstract keyword. Then you override it in a descendant class with Override.

Answer:
  • True
  • False

  •  KB Article #101344 Counter
    33281
    Since 12/23/2008
    Follow PrestwoodBoards on: 


    ©1995-2019 PrestwoodBoards  [Security & Privacy]
    Professional IT Services: Coding | Websites | Computer Tech