Introduction of Abstract classes and Abstract methods of c#
abstract keyword is used to declare abstract classes and abstract methods in c#.
Abstract class
Ø The concept of Abstract Class and Abstract method is used under the concept of Inheritance. The abstract class can only be a base class of other classes.
Ø Declaring class as abstract simply means this class cannot be instantiated , In simple words its object cannot be created. As the instance cannot be created of this abstract class but can inherit another class and by creating the instance of this derived class you can access the method of the abstract class.
Ø Abstract class plays a role when in lot of cases we don't want to create object of specific classes but still we want to take some base functionalities of that base class for its derived classes.
Ø Abstract classes may or may not contain any concrete and even abstract methods.
Ø Abstract classes are templates for future specific classes.
.
Abstract method
Ø Abstract methods are only declared in abstract classes.
Ø Abstract method or abstract property just indicates that the method or property does not contain any implementation.
Ø Because of no actual implementation in declaration of abstract method , it does not contain any method body and simply ends with a semicolon without opening and closing curly braces.
Ø Abstract methods and properties must be implemented in derived classes of abstract class.
Ø Abstract method is implicitly a virtual method.
Ø If you create a abstract method then you must override this method in the subclass otherwise it shows error in the program.
Program to understand the concept of Abstract class.
public abstract class Organization
{
//...Class implementation
public abstract void Salary(int x, int y)
{
//this method mustn't be implemented here.
//If we do implement it, the result is a Syntax Error.
}
}
public abstract Department1 : Organization
{
//...Class implementation
//...you do not have to implement the the method Salary(int x, int y)
}
public class Designation : Department1
{
//here we should provide an implemetation for Salary(int x, int y)
public override void Salary(int x, int y)
{
//must do some Calculation here
}
}
No comments:
Post a Comment