OOP: Encapsulation

Please see my other Object-Oriented Programming (OOP) articles.

Leveraging Encapsulation to Protecting Data & Behavior

As demonstrated in my Inheritance article, I designed an Employee class which serves as a parent class for the Supervisor class.

As you can see, the Employee class contains data and behaviors common to all types of employees, which the Supervisor class reuses in its objects.

Code reuse is just one reason for defining elements common to all child objects in a parent class.
Hiding the contents of a class when possible provides better security and a standard API for code use.
Encapsulation demonstrates this characteristics by limiting access to data using the private keyword and exposing special methods which directly access data in a safe and predefined manner.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s