1.What is abstract class?
-A abstract class is similar to the class which contains abstract as well as Non abstract method (Normal or concrete Method).
2. What is Abstract Method and Non Abstract Method(Concrete Method)?
Abstract Method- A method which does not have definition and contains abstract keyword before return type ( Here return type is void)
-Example of Abstract method- public abstract void print();
Non-Abstract Method- A method which have definition.
-Example of Non Abstract Method- public void print()
{
}
3. Rules for Writing Abstract Class and Abstract Method-
Abstract method does not definition.
Abstract method contains in abstract class.
It is mandatory to write definition of abstract method in derived class.
Use "override" keyword for writing abstract method definition.
For e.g. public override void print() { }
4. When to use Abstract Class?
-When we want to move the common functionality of 2 or more related classes into a base class and when we don't want that base class to be instantiated.
For Example:
Consider Example of Employee, Name is common property for all Employee. so we can write it in abstract class. But there are some Full Time and Part Time Employees. so their salary is calculated differently. that is why we can write two methods as abstract and their implementation in separate classes.
abstract class Employee
{
//Normal Method or concrete Method
public void Name(int name)
{
Console.WriteLine("Name of Employee :"+name);
}
//Abstract Methods
public abstract void FullTime();
public abstract void PartTime();
}
class FullTimeEmployee : Employee
{
public override void FullTime()
{
Console.WriteLine("Full Time Employee Salary ::");
}
}
class PartTimeEmployee : Employee
{
public override void PartTime()
{
Console.WriteLine("Part Time Employee Salary ::");
}
}
5. How to write Abstract Class and Abstract Method?
If you write Non-abstract method without definition like this public void Print();
You will get error like Error - 'Employee.Print()' must declare a body because it is not marked abstract,extern or partial.
If you write abstract method in Non Abstract class (class without abstract keyword)
class Employee
{
//Normal Method or concrete Method
public void Name(int name)
{
Console.WriteLine("Name of Employee :" + name);
}
//Abstract Method
public abstract void Print();
}
then you will get error like this -
Error - Employee.Print()' is abstract but it is contained in non-abstract class 'Employee' .
It is mandatory to have implementation of abstract method in derived class otherwise you will get following error
Error - FullTimeEmployee' does not implement inherited abstract member .Employee.FullTime()'
I have written public abstract void FullTime(); abstract method in Employee class and Employee class is inherited in FullTimeEmployee. I have commented the definition of definition of FullTime() Method.
abstract class Employee
{
//Normal Method or concrete Method
public void Name(int name)
{
Console.WriteLine("Name of Employee :" + name);
}
//Abstract Methods
public abstract void FullTime();
}
class FullTimeEmployee : Employee
{
//public override void FullTime()
//{
// Console.WriteLine("Full Time Employee Salary ::");
//}
}
We can not create instance of the Abstract Class.
If You try to create Instance of the abstract class then you will get following error
Error - Cannot create an instance of the abstract class or interface 'Employee'
In out Example Employee is the abstract class.
class Program
{
static void Main(string[] args)
{
Employee objEmp = new Employee();
}
}
Abstract class can be used as a base class.
Abstract class can't be sealed type. (Sealed type means we can derive new class from sealed class.)
Example of Abstract Class with Sealed Keyword-
abstract sealed class Employee
{
//Normal Method or concrete Method
public void Name(int name)
{
Console.WriteLine("Name of Employee :" + name);
}
//Abstract Method
public abstract void FullTime();
}
Error in above Example - Employee': an abstract class cannot be sealed or static.
We can write property inside abstract class.
We can declare default and parameterized constructor inside abstract class.
No comments:
Post a Comment