Abstract Class:
Abstract class is same as a simple class with abstract
keyword. In abstract class some members of a class have method declaration and
some have implementation. So abstract class same as interface but in interface
not even single member have implementation but in the case of abstract class we
can implement some members.
You can declare abstract class like below code snippet:
public abstract class DisplayEmployee
{
}
Abstract members of abstract class cannot be
private.
See below code snippet:
using System;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Employee Emp = new
Employee();
Emp.getEmployee();
Console.Write(Emp.displayEmployee() + "\n");
Console.ReadLine();
}
}
public abstract class DisplayEmployee
{
public abstract int displayEmployee();
public void
getEmployee()
{
Console.Write("Get
Employee \n");
}
}
class Employee : DisplayEmployee
{
int EmployeeId = 0;
public void
getEmployee()
{
EmployeeId = 123;
}
public override int displayEmployee()
{
return EmployeeId;
}
}
}
In the above code we
have implemented abstract class, where you can see there are one abstract
method and one full implemented method. This is main purpose of abstract class.
By creating derived
class object we can access the functionality defined in abstract class.
We can also create instance like this below code snippet:
DisplayEmployee emp = new
Employee();
emp. displayEmployee();
And one more method for
accessing method in abstract class is like below:
Employee employee = new Employee();
((DisplayEmployee)employee).displayEmployee();
Key Points:
1.
Cannot create instance of abstract class or
interface
2.
In abstract class, some members have implementation
also
3. Abstract
members of abstract class cannot be private.
4.
Unlike interface, we can declare variables in
abstract class.
5.
In derived class you must have to specify
“override” keyword for derived members.
0 Comments