Abstraction
- Abstract defines "what something is".
- An abstract is a class which cannot be instantiated.
- It is declared by abstract keyword.
- A class can inherit/implement only one abstract or other class.
- An abstract class can have Fields, Constrants, abstract Properties, non-abstract Properties, Constructor and abstract methods (with body) and non-abstract methods(without body).
- Default Implementation - The default implementation of an abstract class consists of complete methods, default code and/or methods which need to be overridden.
- Access Modifiers - An abstract class can have access modifiers for the subs, functions, properties.
- Where To Use - When common behaviour/status is required in several implementations then abstract class is better to use.
- Adding Functionality - Adding new methods to an abstract class, we have the option of providing default implementation so that all the existing code might work properly.
- Abstract Keyword Implementation - For every abstract properties and abstract methods, it has to be override in a class which inherit from abstract class
Example of an abstract class:
/// <summary>
/// Employee description.
/// </summary>
public abstract class Employee
{
//Abstract class can have fields and properties.
//Fields
#region Fields
protected String id;
protected String firstName;
protected String lastName;
#endregion
//properties
#region Properties
public abstract String ID
{
get;
set;
}
public abstract String FirstName
{
get;
set;
}
public abstract String LastName
{
get;
set;
}
#endregion
//completed methods
#region Completed Methods
//completed methods
public String Add()
{
return "Employee " + id + " " + firstName + " " + lastName + " added";
}
//completed methods
public String Delete()
{
return "Employee " + id + " " + firstName + " " + lastName + " deleted";
}
//abstract method that is different therefore i keep it uncompleted.
public abstract String CalculateWage();
}
Interface
- An interface defines "How it behave" ("what something can do").
- An interface is not a class. It is an entity.
- It is declared by interface keyword.
- A class can inherit/implement multiple/several interfaces.
- An interface CANNOT have Fields, Constrants and Constructors, it can have only signature/declaration of Properties and Methods (definition of the methods without the body).
- Default Implementation - An interface cannot provide any code, just the signature.
- Access Modifiers - Everything is public in an Interface. An interface cannot have access modifiers for the subs, functions, properties.
- Where To Use - When some methods signature are required to expose in the implementation then it is better to use Interfaces.
- Adding Functionality - Adding new method to an Interface, we have to define in all implementations of an interface for the new method.
- Interface Implementation - For every properties and methods, it has to be implemented in a class which inherit from an interface.
Example of an interface:
/// <summary>
/// IEmployee description
/// </summary>
public interface IEmployee
{
//cannot have fields. So uncommenting will raise error!
//protected String id;
//protected String firstName;
//protected String lastName;
//just signature of the properties and methods.
//setting a rule or contract to be followed by implementations.
String ID
{
get;
set;
}
String FirstName
{
get;
set;
}
String LastName
{
get;
set;
}
// cannot have implementation
// cannot have modifiers public etc all are assumed public
// cannot have virtual
String Add();
String Delete();
String CalculateWage();
}
Inherited Objects
/// <summary>
/// A class which inherits an abstract class
/// </summary>
public class Emp_InheritAbstract : Employee
{
//uses all the properties of the abstract class therefore no properties or fields here!
public Emp_InheritAbstract()
{
}
public override String ID
{
get { return id; }
set { id = value; }
}
public override String FirstName
{
get { return firstName; }
set { firstName = value; }
}
public override String LastName
{
get { return lastName; }
set { lastName = value; }
}
//common methods that are implemented in the abstract class
public new String Add()
{
return base.Add();
}
//common methods that are implemented in the abstract class
public new String Delete()
{
return base.Delete();
}
//abstract method that is different therefore I override it here.
public override String CalculateWage()
{
return "Employee " + base.firstName + " is calculated " + "using the Abstract class...";
}
}
///
/// A class which inherits and implements an interface
///
public class Emp_InheritInterface : IEmployee
{
//All the properties and fields are defined here!
protected String id;
protected String lname;
protected String fname;
public Emp_InheritInterface()
{
//
// TODO: Add constructor logic here
//
}
public String ID
{
get { return id; }
set { id = value; }
}
public String FirstName
{
get { return fname; }
set { fname = value; }
}
public String LastName
{
get { return lname; }
set { lname = value; }
}
//All the manipulations including Add, Delete, Calculate are done
//within the object as there are not implementation in the Interface entity.
public String Add()
{
return "Employee " + fname + " added.";
}
public String Delete()
{
return "Employee " + fname + " deleted.";
}
//if you change to Calculatewage(). Just small 'w' it will raise error as in interface
//it is CalculateWage() with capital 'W'.
public String CalculateWage()
{
return "Employee " + fname + " caluculated using " + "Interface.";
}
}
Code for Testing
//Code for Testing
public class AbstractsANDInterfaces
{
//This is the sub that tests both implementations using Interface and Abstract
public string TestAbstractExample()
{
StringBuilder sb = new StringBuilder();
Employee emp = new Emp_InheritAbstract();
emp.ID = "A01";
emp.FirstName = "Chetan";
emp.LastName = "Sharda";
//call add method the object
sb.AppendLine(emp.Add().ToString());
//call the CalculateWage method
sb.AppendLine(emp.CalculateWage().ToString());
return sb.ToString();
}
public string TestInterfaceExample()
{
StringBuilder sb = new StringBuilder();
try
{
IEmployee emp;
Emp_InheritInterface empFromInterface = new Emp_InheritInterface();
emp = empFromInterface;
emp.ID = "I02";
emp.FirstName = "Chetan";
emp.LastName = "Sharda";
//call add method the object
sb.AppendLine(emp.Add().ToString());
//call the CalculateWage method
sb.AppendLine(emp.CalculateWage().ToString());
}
catch (Exception ex)
{
sb.AppendLine(ex.Message);
}
return sb.ToString();
}
}
Reference:
http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
http://www.c-sharpcorner.com/uploadfile/prasoonk/abstract-class-vs-interface/
0 Comment(s)