From the course: Programming Foundations: Object-Oriented Design

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Encapsulation

Encapsulation - Python Tutorial

From the course: Programming Foundations: Object-Oriented Design

Start my 1-month free trial

Encapsulation

- The idea of encapsulation is about containing the elements of an object. Not just to keep them together, but to also protect them. - Ow! - We bundle an object's attributes or data along with the methods that operate on that data within the sane unit or the same class. One reason for doing that is to restrict access to some of the object's components. - Like how you're restricting me from taking a cookie? - No, you can have a cookie, but you'll have to use my method to get to it. My cookie jar class has an attribute that represents the number of cookies in the jar. When I create an instance of my cookie jar, I don't want you or any other part of my application to be able to reach in and directly change the number of cookies. - Oh, come on. You can trust me. - It's not only about trust. If I let you directly change things, like the number, you might unknowingly change it to something invalid, like a negative one. That value doesn't make sense for the number of cookies and might cause…

Contents