What is Abstraction and Encapsulation

Abstraction:
  1. Hides the implementation details of your methods. Provides a base to variations in the application which can grow over a period of time.

  1. Abstraction is virtual class design.
Before actually defining class, developer will think about what all properties, methods and event will be   there in my class.
Example :
public abstract class Automobile
{
 //Properties
 public int DoorCount { .. }
 public string EngineType { .. }
 public float Height { .. }
 public float GasTankSize { .. }
 public float WheelBase { .. }
 public float WheelSize { .. }

 //Abstract Properties
 public abstract string BodyType { get; }

 //Methods
 public float CalculateFuelEfficiency(int lastMileage);
}
The Automobile class is the interface, in that it defines an interface that classes will derive from. You can't directly instantiate it through new Automobile() because it's not an implementation.
It's important to note the BodyType property. This property is declared abstract, meaning that any derived implementation must override it and return some relevant string.
From the example above, let's say we want a higher level of abstraction

Encapsulation:
  1. Hides the private data elements of the class and exposes only the required things to the clients.
  2. At the time of class definition, developer will think about which should display to end user and which should not.
In Short Abstraction is "Collection of data" and Encapsulation is "Exposure (or grouping) of data in appropriate access specifier".

In this Data is sent to a method, a lot of work goes on using the data, of which you don't know or care about. An output is returned to the caller. That is the process of encapsulation, or information hiding.
Example :
public class MultiLineString
{
 private CollectionBase<string> _lines = null;
 private bool _allowEmptyLines = false;
 private static object _lock = new object();
 public bool AllowEmptyLines
 {
     get { return _allowEmptyLines; }     set { _allowEmptyLines = value; }
 }

 public int LineCount
 {
   get { return _lines.Count; }
 }

 public string this[int index]
 {
   get { return _lines[index]; }
 }
 public MultiLineString(string text) : this(text, false) { }
 public MultiLineString(string text, bool allowEmptyLines)
 {
   _allowEmptyLines = allowEmptyLines;
   _lines = new CollectionBase<string>();
   if (!allowEmptyLines)
   {
     string[] lines = text.Split('\n');
     foreach (string line in lines)
     {
       if (line != null)
         this.Add(line);
       else if (line == '\r' && this.AllowEmptyLines)
         this.Add(string.Empty);
     }  } }
 public MultiLineString(StringBuilder builder) : this(builder, false) { }
 public MultiLineString(
   StringBuilder builder,
   bool allowEmptyLines) : this(builder.ToString(), allowEmptyLines) { }
 public void AddLine(string line)
 {
   if (!this.AllowEmptyLines && string.IsNullOrEmpty(line))
     throw new ArgumentNullException("line");
   lock (_lock)
   {
     _lines.Add(this.TrimLine(line));
   } }
 public void RemoveLine(int index)
 {
   lock (_lock)
   {
     _lines.RemoveAt(index);
   }
 }
 public override string ToString()
 {
   string output = string.Empty;
   foreach (string line in lines)
     output += line + "\n";
   return output;
 }
 private string TrimLine(string line)
 {
   return line.Trim(new char[] { ' ', '\t', '\r' });
 }}

Why is encapsulation important? Using the above example, it would be really easy to change the underlying source to use a database, XML file, text file, or another data store, without breaking any code that the developer may have created.

Comments

Popular posts from this blog

Display multiple marker point on Google Map with same location or same Longitude,Latitude

Implementing Zoom functionality in asp.net

Check Session Timeot for Whole Application