Leveraging Encapsulation to Protecting Data & Behavior
With my class requirements defined in my Class Design – Step 1 – Requirements article, I’m now ready to define the details of my Employee class.
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.