Better Programming

Advice for programmers.

Follow publication

Three Thoughts on Encapsulation

There’s more to it than you think

Nick Hodges
Better Programming
Published in
5 min readNov 4, 2019
Photo by Guillaume Bolduc on Unsplash

Introduction

I remember quite well when the “light bulb went on” when it came to object-oriented programming (OOP). It happened while I was reading my favorite technical book of all time, Borland Pascal 7 Insider by Paul Cilwa.

It was a terrific book, full of excellent writing and exceedingly useful code. It illustrated the use of objects, including encapsulation, inheritance, and the hardest notion — polymorphism — beautifully.

It was a thrill to understand what all the hype was about, finally — and to realize the hype was worth it.

When someone approaches OOP, I think the first and most straightforward concept to get is the notion of encapsulation. In its most basic form, encapsulation is “information hiding” — the concealment of the internal workings of a class.

New developers seem to get that notion pretty quickly. They then move on to inheritance and polymorphism.

Encapsulation looks pretty simple. Here’s an example in TypeScript:

Easy, right? The internal data is private and protected from prying hands, while it is safely exposed as a property. It couldn’t be simpler.

1. It Includes Implementation Hiding

But I think that encapsulation is given short shrift as the “easy and basic” part of OOP. I believe that it is more important and more nuanced than it is given credit for.

Sure, it’s about information hiding. That’s the basic idea, naturally. But there’s more to it. I think that instead of thinking about encapsulation as “information hiding”, we should be thinking about it as “implementation hiding”.

Consider this class:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Nick Hodges
Nick Hodges

Written by Nick Hodges

Just a guy with a bad pistachio addiction.

Responses (1)

Write a response

I've started learning Dart and Flutter in 2022.
I was very interested in the Flutter Flow for a long time, but as someone who really knows how to program, it felt a step back.
A while ago, one of the Flutter community speakers at YouTube interviewed…

--

Leaving Google for FlutterFlow is a bold move!
Your insight into the potential of FlutterFlow is intriguing. Excited to see your journey unfold.
Thank you for sharing your experience and shedding light on this decision. Wishing you success in your new venture!

--

Low code tools like FlutterFlow are already set up for success here. They have previews of the app and allow users to visually select different UI components, which can provide great co...

FlutterFlow enables rapid app development through low-code tools.

--