Encapsulation
Anonymous contributor
Published Aug 2, 2021Updated Mar 7, 2023
Contribute to Docs
Encapsulation advances the benefits of modularity and hiding away of complexities in order to better maintain and reason about code. It is one of the four principles of object-oriented programming (OOP).
Most of the time encapsulation can be achieved by creating classes with an overarching design structure that includes private and public methods (or getters and setters) for our systems to interact.
Python Example
In the following Python example, a Robot
class is created with a __version
property, initialized as a number. Getter and setter methods are made to allow any instance of Robot
to be set with a __version
and have it changed later]:
class Robot(object):def __init__(self):self.__version = 22def getVersion(self):print(self.__version)def setVersion(self, version):self.__version = versionobj = Robot()obj.getVersion()obj.setVersion(23)obj.getVersion()print(obj.__version)
All contributors
- Anonymous contributor
- christian.dinh
- theeguru___
- CaupolicanDiaz
- Christine_Yang
- garanews
- BrandonDusch
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn more on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours