C# Abstract Classes and Abstract Members

In this chapter you will learn:

  1. What are Abstract Classes and Abstract Members
  2. Syntax for C# Abstract Classes and Abstract Members
  3. Example for Abstract Classes and Abstract Members

Description

A class declared as abstract can never be instantiated. Only its concrete subclasses can be instantiated.

Abstract classes are able to define abstract members. Abstract members are like virtual members without a default implementation.

That implementation must be provided by the subclass, unless that subclass is also declared abstract.

Syntax

To declare an abstract method, use this general form:

abstract type name(parameter-list);

Example


using System;//from www.  jav a 2  s .c o  m
abstract class Shape
{
    public abstract int GetArea();

}

class Rectangle : Shape
{
    public int width;
    public int height;

    public override int GetArea()
    {
        return width * height;
    }
}

class Program
{
    static void Main(string[] args)
    {

        Rectangle r = new Rectangle();
        r.width = 5;
        r.height = 6;
        Console.WriteLine(r.GetArea());

    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Polymorphism
  2. Example for Polymorphism
Home »
  C# Tutorial »
    C# Types »
      C# Inheritance
C# Class Inheritance
C# base Keyword
C# seal Functions and Classes
C# Abstract Classes and Abstract Members
C# Polymorphism
C# Virtual Function Members
C# Override methods
C# Shadow inherited members
C# as operator
C# is operator