Abstract classes can simply be defined as incomplete classes. Abstract classes contains one or more incomplete methods called abstract methods. The abstract class only provides the signature or declaration of abstract methods and leaves the implementation of these methods to derived or sub-classes. Since abstract classes are incomplete they cannot be instantiated. They must be inherited in order to use their functionality. This is the reason why an abstract class can't be sealed. A class inheriting an abstract class must implement all the abstract method in the abstract class, or it too must be declared as an abstract class.
We can declare a reference of type abstract class and it can point to the objects of the classes that have inherited the abstract class
Example:
Features of Abstract Class
We can declare a reference of type abstract class and it can point to the objects of the classes that have inherited the abstract class
Example:
abstract class TaxCalculator
{
protected double itemprice;
protected double tax;
public abstract double CalculateTax();
public double Tax
{
get { return tax; }
}
public double ItemPrice
{
get { return itemprice; }
}
}
{
protected double itemprice;
protected double tax;
public abstract double CalculateTax();
public double Tax
{
get { return tax; }
}
public double ItemPrice
{
get { return itemprice; }
}
}
Class SalesTaxCalculator : TaxCalculator
{
public SalesTaxCalculator(double itemprice)
{
this.itemprice = itemprice;
}
public override double CalculateTax()
{
tax = 0.3 * itemprice;
return itemprice + tax;
}
}
class Test
{
static void Main()
{
SalesTaxCalculator salesTaxCalc = new SalesTaxCalculator(225);
double newprice = salesTaxCalc.CalculateTax();
//Or we can do it as
TaxCalculator taxCalc = new SalesTaxCalculator(225);
double tprice = taxCalc.CalculateTax();
}
}
Features of Abstract Class
- An abstract class cannot be instantiated.
- An abstract class contain abstract members as well as non-abstract members.
- An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
- A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
- An abstract class can be inherited from a class and one or more interfaces.
- An Abstract class can have access modifiers like private, protected, internal with class members. But abstract members cannot have private access modifier.
- An Abstract class can have instance variables (like constants and fields).
- An abstract class can have constructors and destructor.
- An abstract method is implicitly a virtual method.
- Abstract properties behave like abstract methods.
- An abstract class cannot be inherited by structures.
- An abstract class cannot support multiple inheritance.
- Don't define public constructors within abstract class. Since abstract class cannot be instantiate and constructors with public access modifiers provides visibility to the classes which can be instantiated.
- Define a protected or an internal constructor within an abstract class. Since a protected constructor allows the base class to do its own initialization when sub-classes are created and an internal constructor can be used to limit concrete implementations of the abstract class to the assembly which contains that class.
No comments:
Post a Comment