Abstraction is a process of displaying only
required information and hiding rest of the details.(Hiding Implementation)
To Abstract something we have to hide the details
of something or to hide the details of something we have to abstract it.
Information hiding is the Goal, Abstraction is a
Process
Example - A Person
using a Mobile Phone need not know details about Motherboard or IC's
(Integrated Circuit) inside it, To use it, we just need to know how to operate
the Mobile phone by switching it on, we don't need to know how the internal
parts are working.
Abstraction can be achieved in 2 ways
1. Using Access Modifiers
2. Using Abstract Class and Interface
Abstraction using Access Modifiers
Example - Class exposing required methods, properties using access modifiers to achieve abstraction.
using
Abstraction;
using System;
namespace Abstraction
{
public class Mobile
{
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
MobileDetails()
{
Console.WriteLine("Brand: " + Brand);
Console.WriteLine("Model: " + Model);
}
public void
PowerButton()
{
Console.WriteLine("To turn on the Mobile");
}
private void
MotherBoardDetails()
{
Console.WriteLine("MotherBoard Details");
}
private void
ICDetails()
{
Console.WriteLine("IC [Integrated Circuit] Details");
}
}
}
In the above code -
We have defined the properties, methods that are
required to be exposed using public modifiers, and we are using private
modifier to hide the details that need not be exposed to outside world, this
way we have achieved abstraction by displaying essential information and hiding
rest of the details.
The below methods are not required to be shown as
this is internal part of phone (Private).
private void MotherBoardDetails()
private void ICDetails()
The below properties and methods are required to be
shown (Public).
public string Brand
public string Model
public void PowerButton()
public void MobileDetails()
If we create instance of Mobile
class , we can access Brand, Model, PowerButton and MobileDetails but not
MotherBoardDetails and ICDetails as per the image below.
0 comments: