We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept
Card image cap

Object Oriented Programming Concepts - Encapsulation

OOP Concepts  • Posted 2 months ago

Encapsulation can be simply stated as "hiding entities which are not required for the context". In other words, it means to deny access to those features or contents of it which aren't needed for the other components to get hold of. The client or the requesting component need not know all the details of the component it is requesting, it just needs to be informed of only things it requires to get things done.

Another explanation for Encapsulation is that to wrap all the information and functionality which represent a responsibility together under a logical structure. This logical structure provides the necessary housing for all the elements under it from any unwanted access, thereby acting as a barrier for the outside.

Encapsulation in its basic idea -

  1. Wraps up all things which share a common responsibility
  2. Hides these things from any unwanted access

The logical structure which provides the housing for all these entities is called as a Class, and any component which needs to access these entities wrapped within a Class by means of a physical reference called as an Object.

"A Class provides a logical structure and a lock, while the object for a class is the key to its access"

In the previous example of a MusicPlayer class,

the information (represented by the variables) and the functionality (represented by the methods) share a common responsibility: simulating the work of a Music Player. And so its legit to wrap all these items together by a logical structure aka a class by the name MusicPlayer.


public class MusicPlayer
{
    private List<string> songsLibrary;
    private List<string> playQueue;
    bool isPlaying;
    
    public MusicPlayer(ILibraryService service)
    {
        // initialization functionality
    }

    public void pause()
    {
        // functionality
    }

    public string resume()
    {
        // functionality
    }

    public void play(string songName)
    {
        // functionality
    }

    public IEnumerable<string> search(string keyword)
    {
        // functionality
    }

    public void stop()
    {
        // functionality
    }
}

This class MusicPlayer creates a logical grouping for all functionalities and related information under a single entity called "class MusicPlayer" which can be accessed from the outside when needed by means of an instance of this "class". An instance for this "class" is created as:


MusicPlayer musicPlayer = new MusicPlayer();

Whenever we use a "new" keyword followed by the class type, we're literally creating a "physical copy" of this otherwise logical entity in the memory and accessing the functionality and data out of it.

"An Object is a physical copy of the Class which is a logical structure"

Also, observe the keywords which prefix the methods and fields inside the class: public, private. These are called "Access Modifiers" which specify the accessibility of a particular field or method inside a class from an outside world perspective.

Generally speaking, there are four kinds of access modifiers which define whether a field or a method is deemed to be accessed from out of class or not; if accessible, how far the accessibility can be.

The scope of accessibility can be: local, referential, inherited

They are:

  • public - accessible from outside of a class, package or a subclass (local, referential and inherited)

  • private - accessible from only within the class where the field or method is specified and nowhere else (local only)

  • protected - accessible from within the class and within the derivative subclasses only (local, inherited only)

  • default / no modifier - the default access specifier provides access from within class only (local only)

Final Words:

  1. Encapsulation provides a logical grouping for the data and functionality which serve a single responsibility
  2. It also provides information hiding from unwanted access
  3. The logical grouping is done by "classes" and are accessed by "objects"
  4. The access modifiers provide necessary locks over a class contents from outside access in public, private or protected terms