#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171

    Encapsulation: A mechanism for restricting access to some of the class's components.


    I see and undertsnad how things work with public, protected and private in classes inheritance etc.

    I am looking for a simple way to write a definition for encapsulation. I have found these. Which one you think is the closest.
    Please write your definition as well; in total 'moron language'.

    Thank you
    Encapsulation is a way of storing an object or data as a property within another object, so that the outer object has full control over what how the internal data or object can be accessed
    Encapsulation is just when you want to hide stuffs into your object from the public. It's the idea of hiding information.
    A mechanism for restricting access to some of the object's components.
    A mechanism for restricting access to some of the class's components.
    Encapsulation is about grouping of functionality (operations) and related data (attributes) together into a coherent data structure (classes).
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    There is no "official" definition for the term encapsulation, so how are we supposed to decide which one is closest? Closest to what?

    All of those descriptions are valid in the sense that they reflect what many people mean when they talk about "encapsulation". The last one (grouping of functionalities) describes a completely different concept than the previous ones (information hiding), but according to Wikipedia, some people call this "encapsulation" as well.

    Don't ask about words, ask about concepts. One concept of OOP is that objects hide their internal data and functionalities. This is a very natural concept. It's how big organizations work. If you work at a big software company, for example, the boss isn't interested in what IDE you use or how you've set up your computer. That's your private stuff. What the boss is interested in is giving you a task and getting back code. How you write this code is irrelevant.

    So if you were an object, you'd look something like this:

    PHP Code:
    <?php

    class Programmer {

        public function 
    write_code($task) {
            
    $this->start_computer();
            
    $this->start_ide();
            
    $code $this->start_typing();
            ...
            return 
    $code;
        }

        private 
    start_computer() {
            ...
        }
        
        private 
    start_ide() {
            ...
        }
        
        private 
    start_typing() {
            ...
        }

        private 
    drink_coffee() {
            ...
        }

        ...    

    }
    And all the boss sees from you is this:

    PHP Code:
    <?php

    $english_breakfast_tea 
    = new Programmer();

    $code $english_breakfast_tea->write_code('Implement a chat for our new website.');
    The outside world is only interested in delegating a task to you and getting back a result. How exactly you complete this task (starting the computer, typing on the keyboard, drinking coffee from time to time) is up to you.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo