Encapsulation:
Here, the public modifier is used to allow a defined fields, properties and methods to access outside of the class and the private modifier is used to hide or restrict an access of required fields, properties and methods from the outside of class.
Encapsulation is a process of binding the data members and member functions into a single unit. In c#, class is the real time example for encapsulation because it will combine a various type of data members and member functions into a single unit.
If we define a class fields with properties, then the encapsulated class won’t allow us to access the fields directly, instead we need to use getter and setter functions to read or write a data based on our requirements.
Example:
Following is the example of defining an encapsulation class using properties with get and set accessors.
using System;
using System.Text;
namespace Tutlane
{
class User
{
private string location;
private string name;
public string Location
{
get
{
return location;
}
set
{
location = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
class Program
{
static void Main(string[] args)
{
User u = new User();
// set accessor will invoke
u.Name = "Suresh Dasari";
// set accessor will invoke
u.Location = "Hyderabad";
// get accessor will invoke
Console.WriteLine("Name: " + u.Name);
// get accessor will invoke
Console.WriteLine("Location: " + u.Location);
Console.WriteLine("\nPress Enter Key to Exit..");
Console.ReadLine();
}
}
}
If you observe above example, we defined a fields in encapsulated class using properties and we are able to manipulate field values using get and set accessors of properties.
Abstraction:
In c#, Abstraction is a principle of object oriented programming language (OOP) and it is used to hide the implementation details and display only essential features of the object.
In Abstraction, by using access modifiers we can hide the required details of object and expose only necessary methodsand properties through the reference of object.
In real time, laptop is the perfect example for abstraction in c#. A laptop which consists of many things such as processor, RAM, motherboard, LCD screen, camera, USB ports, battery, speakers, etc. To use it, we just need to know how to operate the laptop by switching it on, we don’t need to know how internally all the parts are working. Here, the laptop is an object which is designed to expose only required features by hiding its implementation details.
In object oriented programming, class is the perfect example for abstraction. In c#, we can create a class with required methods, properties and we can expose only necessary methods and properties using access modifiers based on our requirements.
Following is the example of defining a class with required methods, properties and exposing it by using access modifiers to achieve abstraction functionality.
public class Laptop
{
private string brand;
private string model;
public string Brand
{
get { return brand; }
set { brand = value; }
}
public string Model
{
get { return model; }
set { model = value; }
}
public void LaptopDetails()
{
Console.WriteLine("Brand: " + Brand);
Console.WriteLine("Model: " + Model);
}
public void LaptopKeyboard()
{
Console.WriteLine("Type using Keyword");
}
private void MotherBoardInfo()
{
Console.WriteLine("MotheBoard Information");
}
private void InternalProcessor()
{
Console.WriteLine("Processor Information");
}
}
If you observe above code, we defined a Laptop class with required fields, properties and methods with public, private access modifiers to achieve an abstraction functionality by hiding and exposing some of methods and properties based on our requirements.
No comments:
Post a Comment