- Back to Home »
- C#.NET »
- C#.NET Abstract Class
Posted by :
Sudhir Chekuri
Tuesday, 5 January 2016
Abstract class is a class with both abstract methods and normal methods.
Abstract methods will have only definition and they will not have implementation.
A class that inherits the abstract class should implement the abstract methods in the abstract class by overriding it.
Example program for abstract class
Program
Output
Area of the square = 144
Abstract methods will have only definition and they will not have implementation.
A class that inherits the abstract class should implement the abstract methods in the abstract class by overriding it.
Example program for abstract class
Program
using System;
abstract class ShapesClass
{
abstract public int Area();
}
class Square : ShapesClass
{
int side = 0;
public Square(int n)
{
side =
n;
}
// Area method is required to
avoid
// a compile-time error.
public override int Area()
{
return side * side;
}
static void Main()
{
Square sq = new Square(12);
Console.WriteLine("Area of the square =
{0}", sq.Area());
Console.ReadKey();
}
}
Output
Area of the square = 144