Another big concept associated with object-oriented programming is encapsulation. Every class contains some data in its fields. We hide these fields by declaring them private. We define getter/setter methods to access them.

 When we assign a value to a field, do we validate the value before assigning it? By providing a setter method, we get an opportunity to provide such a validation. As an example, consider the definition of a Date class declared as follows:

class Date
     public int day;      public int month;
     public int year;


This class declares three public fields. Although it is against the object-oriented programming best practices, these fields are declared public to illustrate the problems associated with public declarations. If d is an object of the Date class, we could use the following statement to set its month value:

d.month = 13;

The code is syntactically correct and will execute without any errors. However, this is obviously a wrong assignment, which may be due to a simple typing error. To protect from such not-so-inevitable errors, be sure to make:

·     Our fields private

·     Define a setter method to assign values to them

·     Provide the validation in the setter methods before making an assignment.

Thus, we could write the setter method for the month field as follows:

public void setMonth(int month)
              if (month >= 1 && month <= 12)                 {
                    this.month = month;
                } else {
                        // print an error message to the user

 This implementation checks whether the value we are trying to assign is within the bounds of a calendar month. This ensures that an invalid value cannot be assigned to the month field. Similar validations can be performed in the setter methods for the day and year fields.

 The process of putting the data and the methods that operate on this data together in the class definition is called encapsulation. As we saw, encapsulating getter/setter methods helps in creating robust objects. When we talk about encapsulation, we are not just referring to the getter/ setter methods. There could be other methods in the class that logically belong to it. For example, let’s take a method called printDate that prints the values of the date attributes in some predefined date format. It makes perfect sense to declare and define the printDate method in the Date class itself. This is exactly what encapsulation is. We encapsulate the methods in a class definition that operate on the class’s fields and make logical sense being in the class definition. The Date class, for example, could provide several methods, such as for the addition of dates, determining whether the current date is in a leap year, and so on. These should be defined in methods that belong to the Date class.

Here’s something else to consider about using encapsulation: If we do not encapsulate the appropriate methods in a class definition, we will have to provide error-checking code in several places within our application. For example, if you do not provide the validations in the setter methods of the Date class, you will need to repeat the validation code in several places in our application, and if this validation ever changes, we will have the big task of ensuring that it occurs in every place we have set the field values.

  Modified On Mar-14-2018 05:37:40 AM

Leave Comment