C# Abstract Classes and Abstract Members
In this chapter you will learn:
- What are Abstract Classes and Abstract Members
- Syntax for C# Abstract Classes and Abstract Members
- 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: