Abstract class
In this chapter you will learn:
What is abstract class
Abstract class is a class with abstract methods. An abstract method cannot have method body and an abstract properties cannot have logics.
We create abstract methods or properties by using the abstract keywords. An abstract method or properties is automatically virtual.
The abstract modifier cannot be applied to static methods.
A class containing abstract methods must be declared as abstract with the abstract specifier.
If a derived class doesn't implement all of the abstract methods in the base class, then the derived class must also be specified as abstract.
To declare an abstract method, use this general form:
abstract type name(parameter-list);
using System;//from java 2s . com
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:
Home » C# Tutorial » Class