Welcome Guest ( Log In | Register )




                Web Hosting Guide

 
Reply to this topicNew Topic
C# Tutorial : Lesson 5 - Encapsulation & Abstraction
turbopowerdmaxst...
post Apr 6 2007, 07:59 PM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 449
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322
myCENTs:30.99


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.



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.

    CODE
    class MyClass
    {

    }


    CODE
    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:-

CODE
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

This post has been edited by turbopowerdmaxsteel: Jun 9 2007, 04:12 AM
Go to the top of the page
 
+Quote Post

Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts 8 nakulgupta 7,985 23rd August 2010 - 07:50 PM
Last post by: iG-
No New Posts
4 PHP
15 Houdini 9,704 20th August 2010 - 05:40 PM
Last post by: iG-kevinanchi
No new   24 mzwebfreak 17,084 13th August 2010 - 10:56 PM
Last post by: iG-
No New Posts   8 jord2 6,674 2nd August 2010 - 12:25 AM
Last post by: iG-Justin Gnatiuk
No New Posts   11 t3jem 4,173 22nd July 2010 - 07:09 PM
Last post by: iG-Rodrigo
No New Posts   2 oval 1,973 2nd July 2010 - 09:28 AM
Last post by: iG-Jhon
No new   19 Dream 18,053 29th June 2010 - 03:31 AM
Last post by: iG-Mystery guy
No New Posts   14 dhanesh 7,519 4th June 2010 - 01:04 PM
Last post by: iG-Nitin Vyas
No New Posts   2 t3jem 4,285 3rd June 2010 - 09:56 PM
Last post by: yordan
No New Posts   3 turbopowerdmaxsteel 9,543 27th May 2010 - 11:31 AM
Last post by: iG-prosper
No New Posts 6 robinhood 6,866 7th April 2010 - 07:01 PM
Last post by: wutske
No New Posts 15 miCRoSCoPiC^eaRthLinG 17,323 27th March 2010 - 07:44 AM
Last post by: iG-Rahul
No New Posts   1 Big_Red2009 8,491 19th March 2010 - 06:35 PM
Last post by: iG-Ananth
No New Posts   4 Klaas 3,443 16th February 2010 - 09:11 PM
Last post by: iG-mubassir ansaro
No New Posts   6 dhanesh 3,326 4th February 2010 - 10:58 AM
Last post by: iG-Sam


Web Hosting Powered by ComputingHost.com.
HONESTY ROCKS! truth rules.
Creative Commons License