Jump to content
xisto Community
turbopowerdmaxsteel

C# Tutorial : Lesson 5 - Encapsulation & Abstraction

Recommended Posts

Encapsulation & Abstraction

 

Two concepts that go together in the object oriented approach are Encapsulation & Abstraction. Abstraction is the representation of only the essential features of an object, while Encapsulation is the hiding of the non-essential features.

 

Think of a person driving a car. He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation). Instead, he needs to know things such as how much turning the steering wheel needs, etc (Abstraction).

 

Consider the example from the programmerâs perspective who wants to allow the user to add items to a list. The user only needs to click a button to add an item (Abstraction). The mechanism of how the item is added to the list is not essential for him (Encapsulation).

 

Below is a graphical representation of Encapsulation for the car class.

 

Posted Image

 

Implementing Encapsulation & Abstraction

Consider the countless advertisements being bombarded to viewers. The viewer does not listen to each one of them, rather only those heâs curious about. We use access specifiers to restrict the scope (visibility) of the class members.

 

Types of Access Specifiers

public - The members (Functions & Variables) declared as public can be accessed from anywhere.

 

private - Private members cannot be accessed from outside the class. This is the default access specifier for a member, i.e if you do not specify an access specifier for a member (variable or function), it will be private. Therefore, string PhoneNumber; is equivalent to private string PhoneNumber;

 

protected - Protected members can be accessed only from the child classes.

 

internal - Internal members can be accessed from anywhere within the application. This is the default access specifier for a class.

 

Both these classes are equivalent.

 

class MyClass{}

internal class MyClass{}
protected internal - Similar to protected, protected internal members can be accessed from the child classes but only within the application.

Note: The members are always accessible to its own member functions, irrespective of the access specifier.

 

Lets see an example:-

 

class House{	// Data Members	public string DrawingRoom;	private string Kitchen; 	// Member Functions	private bool EnterDrawingRoom()	{		DrawingRoom = "The Drawing Room";	}	public bool EnterKitchen()	{		Kitchen = "The Kitchen"; /* Even though Kitchen is declared Private, EnterKitchen function		can modify it because the member function itself has access to all other members. */	}} class MainClass{	public static void Main()	{		House TheBlackHouse = new House();		TheBlackHouse.DrawingRoom = "Main Drawing Room"; /* OK - Public Data Member is accessible		from outside the class */		TheBlackHouse.Kitchen = "Main Kitchen"; /* Error - Private Data Member is not accessible		 from outside the class. */		TheBlackHouse.EnterDrawingRoom(); /* Error - Private Member Function is not accessible		// from outside the class. */		TheBlackHouse.EnterKitchen(); /* OK - Public Data Member is accessible from outside		the class. */	}}
Lets study the code. Inside the main function we declare an object TheBlackHouse of the House class. Thereafter, we set the value of the data member DrawingRoom for TheBlackHouse to "Main Drawing Room". Since, DrawingRoom is declared as public, the above step works without any error. On the next line, TheBlackHouse.Kitchen = "Main Kitchen"; we are trying to assign the value "Main Kitchen" to the Kitchen variable of TheBlackHouse object. However, Kitchen is declared as private. Hence, this step generates an error.

 

In the next line, we are trying to call the private method EnterDrawingRoom for the object. This generates an error as well. Finally, we are calling the EnterKitchen function which is declared as public. Notice that inside the EnterKitchen function, we are assigning the value "The Kitchen" to the private variable Kitchen. This does not generate an error because the EnterKitchen method belongs to the same class and it has access to all the members inside the class, irrespective of the access specifier.

 

Why do we need Encapsulation & Abstraction?

 

Programming is all about concepts, different from coding where we basically translate the logic to language specific codes. When given a problem statement, there might be a number of ways to solve it. The logic devised to solve it will depend of the programmer. What we must learn to implement is the most efficient approach; efficient in terms of time, memory & cost. Object oriented methodologies such as these are what allow us to maintain efficiency.

 

By using encapsulation & abstraction, we reduce complexity by ignoring unimportant details and we maintain effectiveness by representing important ones.

 

Previous Tutorial - Lesson 4 - Object Oriented Programming

 

Next Tutorial - Lesson 6 - Creating Value Types & Reference Types - Part I

 

Lesson: 1 2 3 4 5 6 7

Edited by turbopowerdmaxsteel (see edit history)

Share this post


Link to post
Share on other sites

Encapsulation - is the ability of an object to hide its data and methods from the rest of the world. It is one of the fundamental principles of OOPs.

 

Abstraction :

 

Abstraction is thinking about something a certain way

Abstraction is the representation of only the essential features of an object and hiding un essential features of an object.

 

More Details : Encapsulation

Notice from Yordan:
Please do not post text copied from somewhere else without properly quoting it ;)

Share this post


Link to post
Share on other sites

Notice from Yordan:
Please do not post text copied from somewhere else without properly quoting it ;)

Its an amazing tutorial for beginners. can you please help me in selecting the right book. Plz recommend a book to continue from chapter 7 on ward. i know there is more to do in csharp but i can't find tutorial chap 8. thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.