Abstract Class Implementing Interface.
Recenlty I took a technically test and I have write an employee class for an aspx page that will
use an abstract class that implements an Interface.
What is abstract class ?
Abstract class is a base class or a parent class. Abstract classes can have empty abstract methods
or it can have implemented methods which can be overridden by child classes.
What are interfaces?
Interface is a contract class with empty methods, properties and functions.
Any class which implements the interface has to compulsory implement all the
empty methods, functions and properties of the interface.
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Employee.Interface
{
public interface IEmployee
{
String Name
{
get;
set;
}
String DateJoin
{
get;
set;
}
String Department
{
get;
set;
}
bool Save(String mName, String mDate, String mDepartment);
}
}
Abstract Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Employee.Interface;
namespace Employee.Abstract
{
public abstract class AEmployee:IEmployee
{
public abstract String Name
{
get;
set;
}
public abstract String DateJoin
{
get;
set;
}
public abstract String Department
{
get;
set;
}
public abstract bool Save(String mName, String mDate, String mDepartment);
}
}
Employee Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Employee.Abstract;
namespace Employee
{
public class Employee : AEmployee
{
private String _Name = String.Empty;
private String _DateJoin = String.Empty;
private String _Department = String.Empty;
public override String Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public override String DateJoin
{
get
{
return _DateJoin;
}
set
{
_DateJoin = value;
}
}
public override String Department
{
get
{
return _Department;
}
set
{
_Department = value;
}
}
public override bool Save(String mName, String mDate, String mDepartment)
{
bool success = false;
try
{
_Name = mName;
_DateJoin = mDate;
_Department = mDepartment;
//Code to save
//
success = true;
}
catch (Exception)
{
throw;
}
return success;
}
}
}