New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

What is encapsulation?

From: Foundations of Programming: Object-Oriented Design

Video: What is encapsulation?

Next up is the idea of Encapsulation. Think capsule like a space capsule or medication capsule or a food container. This is the idea of surrounding something, not just to keep the contents together, but also to protect those contents. Now, in Object Orientation, this first refers to the idea of taking our attributes and then taking our behaviors and bundling them together in the same unit, the same class. But it's really more than that. We also want to restrict access to the inner workings of that class or any objects based on that class, and this is referred to as information hiding or data hiding.

What is encapsulation?

Next up is the idea of Encapsulation. Think capsule like a space capsule or medication capsule or a food container. This is the idea of surrounding something, not just to keep the contents together, but also to protect those contents. Now, in Object Orientation, this first refers to the idea of taking our attributes and then taking our behaviors and bundling them together in the same unit, the same class. But it's really more than that. We also want to restrict access to the inner workings of that class or any objects based on that class, and this is referred to as information hiding or data hiding.

The principle is that an object should not reveal anything about itself except what is absolutely necessary for other parts of the application to work. Let me give you a simple specific example. If we have this bank account class, well, we don't want some other part of our application to be able to reach in and change the balance of any object without going through the deposit or the withdrawal behaviors. Perhaps those behaviors are supposed to perform auditing and logging of deposits and withdrawals.

So we can hide that attribute, that piece of data. We can control access to it so that it's only accessible from inside that object itself. Sure, we can use the deposit and withdrawal methods from other parts of the application, and they can change the balance, but it can't be directly changed from outside the object. This is one that's also referred to with the idea of black boxing. We are closing off more and more of the inner workings of the object except for those few pieces we decide to make public for, say, input and output.

Think of using a telephone. You want your interaction with a telephone to be as simple as possible. Just use the keypad to make a call. But you don't want to care about how it works internally. If the inner workings of your telephone completely changed, it wouldn't matter as long as you still have the same buttons to press. One of the main benefits with Object Orientation is that it allows us to more safely change the way the object works because we've hidden this data. Perhaps we started off by storing that bank balance as a single floating-point number, and then we decide a little later that it's much better to store it as two integers, one for dollars and one for cents.

Now, if we've restricted direct access to that piece of data, we only have to worry about this one class, and we don't have to worry about breaking the other 17 parts of the application that might have been reaching in to grab it. Now, a common question from folks new to Object-Oriented Programming is but if I am writing these classes, why would I want to hide my own code from myself? And here's the thing, it's not about being secretive, that's not our point. It's about reducing dependencies between different parts of the application, that a change in one place won't cascade down and require multiple changes elsewhere. But how much should you hide? Well, the rule is as much as possible.

As we will see, different languages have different levels of support for this concept, but the idea of encapsulation is that you enclose, you encapsulate your object's attributes and methods, and then you hide everything about that object except what is absolutely necessary to expose. And the effort that we will put in to abstracting and encapsulating our classes can still be very useful when creating other classes, as we'll see with Inheritance.

Show transcript

This video is part of

Image for Foundations of Programming: Object-Oriented Design
Foundations of Programming: Object-Oriented Design

47 video lessons · 46495 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 11m 35s
    1. Welcome
      1m 25s
    2. Who this course is for
      1m 15s
    3. What to expect from this course
      3m 6s
    4. Exploring object-oriented analysis, design, and development
      1m 41s
    5. Reviewing software development methodologies
      4m 8s
  2. 26m 14s
    1. Why we use object-orientation
      2m 42s
    2. What is an object?
      5m 22s
    3. What is a class?
      4m 43s
    4. What is abstraction?
      2m 45s
    5. What is encapsulation?
      3m 45s
    6. What is inheritance?
      3m 35s
    7. What is polymorphism?
      3m 22s
  3. 12m 16s
    1. Understanding the object-oriented analysis and design processes
      4m 13s
    2. Defining requirements
      6m 9s
    3. Introduction to the Unified Modeling Language (UML)
      1m 54s
  4. 23m 35s
    1. Understanding use cases
      6m 11s
    2. Identifying the actors
      4m 16s
    3. Identifying the scenarios
      5m 7s
    4. Diagramming use cases
      4m 18s
    5. Employing user stories
      3m 43s
  5. 16m 36s
    1. Creating a conceptual model
      1m 59s
    2. Identifying the classes
      2m 27s
    3. Identifying class relationships
      2m 38s
    4. Identifying class responsibilities
      6m 43s
    5. Using CRC cards
      2m 49s
  6. 22m 25s
    1. Creating class diagrams
      6m 11s
    2. Converting class diagrams to code
      4m 57s
    3. Exploring object lifetime
      5m 55s
    4. Using static or shared members
      5m 22s
  7. 19m 49s
    1. Identifying inheritance situations
      6m 49s
    2. Using inheritance
      2m 43s
    3. Using abstract classes
      2m 2s
    4. Using interfaces
      4m 20s
    5. Using aggregation and composition
      3m 55s
  8. 9m 23s
    1. Creating sequence diagrams
      5m 18s
    2. Working with advanced UML diagrams
      2m 3s
    3. Using UML tools
      2m 2s
  9. 10m 39s
    1. Introduction to design patterns
      2m 40s
    2. Example: the singleton pattern
      4m 53s
    3. Example: the memento pattern
      3m 6s
  10. 21m 47s
    1. Introduction to object-oriented design principles
      2m 50s
    2. Exploring general development principles
      3m 55s
    3. Introduction to SOLID principles
      6m 43s
    4. Introduction to GRASP principles
      8m 19s
  11. 7m 1s
    1. Reviewing feature support across different object-oriented languages
      3m 50s
    2. Additional resources
      2m 27s
    3. Goodbye
      44s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.