Principles Of Object Oriented Programming - Part One: Encapsulation

The Inquisitive Dev
3 min readOct 2, 2020

--

This article is part of a series that covers the fundamentals of object oriented programming:

Part One: Encapsulation

Part Two: Inheritance

Part Three: Polymorphism

Part Four: Abstraction

As a software developer its is critical to learn the fundamentals of object oriented programming (OOP). Many of the most popular programming languages utilise OOP principles including, Java (which I will use in this article), JavaScript and Python, to name a only a few. These principles will allow you to build robust, flexible and efficient applications in a variety of languages. Developing a foundational knowledge in a new OOP language simply becomes a case of memorising a new syntax.

In the following series I will address a fundamental principle of Object Oriented Programming (OOP) each week, starting this week with encapsulation. Following weeks will cover topics such as Inheritance and overriding, Polymorphism, Abstraction and Interfaces.

This article was originally published on my blog:

Encapsulation

Encapsulation is a mechanism of wrapping an object’s variables (data) and methods (functionality) together as a single unit. The variables of the class will be hidden from other classes through the use of the ‘private’ access modifier. This means they can only be accessed and/or modified by the ‘public’ methods of their current class. Having methods controlling the setting of data means that you can perform validation on the incoming data.

The class below is a good example of encapsulation.

The class fields accountNum and sortCode on line 6 and 7 have ‘private’ access modifiers, meaning they cannot be read or written to directly from outside this class.

The method below is known as a getter. It has a ‘public’ access modifier so external classes can call this method to retrieve the data held in the accountNum field. Next to the access modifier we can see the type of data this method will return, in this case an ‘int’ (integer).

Setters, like ‘setAccountNum’ below, allow external classes to write new data to the accountNum field. The ‘void’ key word tells us that this method doesn’t return any data. Inside the parenthesis, to the right of the method name, we can see the input data type (int) and the internal variable name (accNum) that will hold the input data. This variable is used only inside the setAccountNum method and could be called anything. On the second line we check to see if the value of accountNum is greater than 0. If it is we will assign the value to the accountNum of the Account class. If the input value is less than 0, we do not set the class field and we output a message to the console.

Note: ‘this’ keyword refers to the object that has called the setAccountNum method, so in this case, the Account class.

The code below creates a new Account object and then sets the accountNum field of that object to ‘12345’. It then prints out the value returned by the getter method, which as you’d expect is ‘12345’. It then goes on to call the same setter method again but this time with the value ‘54321’, which will overwrite the first method. When the getter is called a second time the new value will be returned.

So that’s it

This is the encapsulation principle of OOP in a nutshell. It allows the object to keep its state private and gives it control over the data being read and written. Next time I will talk about the all important principle of abstraction.

If you found this article useful there might be more for you on my blog:

Thank you so much for reading.

Ben

--

--