19.13. Encapsulation: Data Structure
Page 74 | Listen in audio
19.13. Encapsulation: Data Structure
Encapsulation is one of the four fundamental principles of object-oriented programming (OOP). It plays a crucial role in managing complexities in software development by allowing data to be bundled with the methods that operate on that data. This is accomplished through the concept of classes and objects.
A class is a data structure that contains data and functions that operate on that data. Data are known as attributes and functions are known as methods. An object is an instance of a class. When an object is created, a new instance of the class is created and the data is encapsulated within the object.
Importance of Encapsulation
The main advantage of encapsulation is the ability to modify our implemented data without affecting the other components of the program. This provides a way to protect the data from being accessed directly by only allowing it to be accessed through methods.
In addition, encapsulation also helps protect data integrity by ensuring that only methods within the class can change data. This prevents data from being changed unexpectedly or incorrectly, which can lead to program errors and inconsistencies.
Encapsulation Implementation
Encapsulation is implemented using access modifiers, which determine the level of access to a class's attributes and methods. There are three main access modifiers: public, private, and protected.
The 'public' modifier allows attributes and methods to be accessed from anywhere in the program. The 'private' modifier restricts access to attributes and methods only to the class where they are declared. The 'protected' modifier allows attributes and methods to be accessed by the class in which they are declared and by any subclasses.
To ensure proper encapsulation, attributes of a class are often declared private, so that they cannot be directly accessed or modified from outside the class. Instead, public methods known as getters and setters are used to access and modify the attributes.
Encapsulation Example
Consider a class 'Car' with attributes 'make' and 'speed'. Attributes are declared as private, while methods 'getBrand', 'setBrand', 'getVelocidade' and 'setVelocidade' are declared as public.
class Car { private String tag; private int speed; public String getMark() { return tag; } public void setBrand(String brand) { this.mark = mark; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } }
Here, the 'getMark' and 'getSpeed' methods are used to access the values of the 'mark' and 'speed' attributes, respectively. The 'setMark' and 'setVelocidade' methods are used to modify the values of the 'mark' and 'velocity' attributes, respectively.
In summary, encapsulation is a fundamental principle of object-oriented programming that helps manage complexity in software development by protecting data integrity and providing a way to modify data without affecting other program components.
p>Now answer the exercise about the content:
What is the main benefit of encapsulation in object-oriented programming?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: