Example of Interface and Abstract class in real project using C# in ASP.Net

Last Reply 9 months ago By Nawaz

Posted 9 months ago

i read also about interface and abstract class.and i know diffrences between both.but i never used these both in my project or any industrial level .so can anyone give me a example in which abstract class and interface are used.please give any example based on any project.or any crud operation?

Posted 9 months ago

Hi iammann,

Here is a small sample that you can understood the concept.

HTML

<div>
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <asp:GridView ID="gvEmployees" runat="server" Caption="<b>Interface</b>" />
            </td>
            <td>
                <asp:GridView ID="gvCustomers" runat="server" Caption="<b>Abstract</b>" />
            </td>
        </tr>
    </table>
</div>

For interface create a property class with the name Employees

/// <summary>
/// Employees get set property.
/// </summary>
public class Employees
{
    public string EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Then create interface IEmployee

/// <summary>
/// interface IEmployee.
/// </summary>
public interface IEmployee
{
    /// <summary>
    /// Get list of Employees.
    /// </summary>
    /// <returns>List of Employees</returns>
    List<Employees> PopulateEmployees();
}

Then create class Employee which inherit interface.

/// <summary>
/// Employee Class inherits IEmployee interface.
/// </summary>
public class Employee : IEmployee
{
    /// <summary>
    /// Get list of Employees.
    /// </summary>
    /// <returns>List of Employees</returns>
    public List<Employees> PopulateEmployees()
    {
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        string query = "SELECT TOP 10 [EmployeeID],[LastName],[FirstName] FROM [Employees]";
        SqlCommand cmd = new SqlCommand(query);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        cmd.Connection = con;
        DataTable dt = new DataTable();
        sda.Fill(dt);
        List<Employees> employees = new List<Employees>();
        foreach (DataRow row in dt.Rows)
        {
            employees.Add(new Employees
            {
                EmployeeId = row["EmployeeId"].ToString(),
                FirstName = row["FirstName"].ToString(),
                LastName = row["LastName"].ToString()
            });
        }
        return employees;
    }
}

For abstract same way create class with the name Customer

/// <summary>
/// Summary description for Customer
/// </summary>
public abstract class Customer
{
    /// <summary>
    /// Gets List of Customers.
    /// </summary>
    /// <returns>List of Customers.</returns>
    abstract public DataTable PopulateCustomers();
}

Create Customers class that override Customer abstract class.

/// <summary>
/// Summary description for Customers
/// </summary>
public class Customers : Customer
{
    /// <summary>
    /// Override PopulateCustomers.
    /// </summary>
    public override DataTable PopulateCustomers()
    {
        string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        string query = "SELECT TOP 10 [CustomerID],[ContactName],[City],[Country] FROM [Customers]";
        SqlCommand cmd = new SqlCommand(query);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        cmd.Connection = con;
        DataTable dt = new DataTable();
        sda.Fill(dt);
        return dt;
    }
}

After running the above below is the output.

Interface
EmployeeIdFirstNameLastName
1 Nancy Davolio
2 Andrew Fuller
3 Janet Leverling
4 Margaret Peacock
5 Steven Buchanan
6 Michael Suyama
7 Robert King
8 Laura Callahan
9 Anne Dodsworth
18 Shashikant Moria
Abstract
CustomerIDContactNameCityCountry
AAFKM Mudassar Khan Warszawa Belgium
ALFKI Maria Boise Austria
ANATR Ana Trujillo México D.F. France
ANTON Antonio Moreno Montréal Brazil
AROUT Thomas Hardy Mannheim Ireland
BERGS Christina Berglund Luleå Italy
BLAUS Hanna Moos Mannheim Finland
BLONP Frédérique Citeaux Strasbourg Finland
BOLID Martín Sommer Madrid Argentina
BONAP Laurence Lebihan Marseille USA
I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html