Explain the use of abstraction with an example using C#.NET.
Abstraction is used to create a common set of methods that might have different specific implementations by subclasses. Abstract class cannot be instantiated and consists of abstract methods without any implementations. Classes inheriting from abstract class must implement all the methods in abstract class.
Public abstract class Shape
{
Private float _area;
Public Float Area
{
Get{return _area;}
Set{_area=value;}
}
Public abstract void CalculateArea();
Class Rect:Shape
{
Private float _height;
Private float _width;
Public Rect(float height, float width)
{
_height = height;
_width = width;
}
Public Float Height
{
Get{return _height}
Set{_height=value;}
}
Public Float Width
{
Get{return _width}
Set{_width=value;}
}
Public override void CalculateArea()
{
This.Area=_height*_width;
}
}
}