Sunday, 28 December 2014

Abstract classes in C#.NET, VB.NET

Introduction: Abstract classes are those classes that can not be instantiated, but they can be inherited by 
other classes.
Description: Abstract classes are those classes that can not be instantiated, but they can be inherited by 
other classes. Abstract classes are base classes and inherited classes are called derived classes.
Abstract class can have both abstract and non abstract method in it. 
When to use: It is use to give common functionality to all inherited classes. it works like a base class.
Implementation:
abstract class MyShapes
{ 
abstract public int MyArea();
}
class Square : MyShapes
{
int side = 0;
 public Square(int n)
{ 
side = n; 
}
// Override Area method
public override int MyArea()
{ 
return side * side; 
}
}
class Rectangle : MyShapes
{
int length = 0, width=0;
public Rectangle (int length, int width)
{
this.length = length;
this.width = width;
// Override Area method 
public override int MyArea()
{ 
return length * width; 
}
}

No comments:

Post a Comment