SlideShare a Scribd company logo
1 of 25
Encapsulation
Michael Heron
Introduction
• One of the things that distinguishes object orientation from
other kinds of programming is the tight coupling between
functions and data.
• This is a principle known as encapsulation.
• In this lecture we are going to talk about why this is such an
important technique.
• And the problems it was designed to resolve.
Scope
• Cast your minds back to a few weeks ago when we talked
about global scope.
• I mentioned this was an easy way to cause problems in large
programs.
• By storing data along with the methods that act on that data
(in a class), it is possible to have access to data without global
scope.
• Variables inside a class have class-wide scope.
Encapsulation
• Encapsulation is the principle of keeping data and functions
together in one unit.
• The class
• By doing this, we have a highly portable unit of data we can
manipulate.
• But only through the functions that we set.
• We can set an interface on an object that permits us fine-
grained control over access.
Back To Our Account Class
class Account {
private:
int balance;
int overdraft;
public:
int query_balance();
void set_balance (int);
int query_overdraft();
void set_overdraft (int);
bool adjust_balance (int);
};
Private and Public
• We set sections of the code to be either private or public.
• private for variables
• public for functions
• These are known as visibility modifiers.
• They change how visible these things are to other pieces of code.
• There exists a third called protected.
• Of more niche benefit.
Private
• Private visibility means that variables (or functions) are
available only to the class in which they are defined.
• We can access balance and overdraft as variables within the
methods of our object.
• We cannot access them outside of that object.
• We do this to make sure that people cannot simply set the
value on our data.
• They have to go through methods we provide
Public
• Public means anything has access.
• It’s the highest level of visibility.
• We reserve this visibility for methods that we don’t mind
everyone being able to use.
• These methods make up our interface to the object.
• We use a pair of such methods around private variables.
• We call these access methods
• Usually a query_x and a set_x.
Impact of Change
• It’s very common when writing a program to have to change
the way things work.
• Alas, this comes with major side-effects for many situations.
• When working with other developers especially, you need to
be wary.
• The interface to the objects you write is a kind of informal
contract with your colleagues.
• It’s important to be respectful when developing.
Impact of Change
• Impact of Change defines how much code is likely to need
changed if you make an adjustment to your code.
• The higher the impact, the more code will need modified.
• Some things are almost always safe to do.
• Add in variables.
• Add in methods
• Some things are almost never safe to do.
• Change what a method does
Impact of Change
• In large object oriented programs, there may be thousands of
classes.
• All interrelated in complex ways.
• It is not possible to know which objects may be using public
methods and variables in your code.
• You have to assume if it can be accessed, it will be accessed.
• Can’t change code indiscriminately.
• That’s not respectful.
Impact of Change
• Instead, what we do is restrict visibility.
• To private, ideally.
• We expose as public only those methods we are happy to
support.
• This limits the impact of change.
• We only need to change things in our own class if we modify
things.
• Important to be able to make adjustments in your code.
• Private access permits this.
What is the benefit?
• Simplify documentation.
• External documentation can focus on only relevant functions.
• Can ensure fidelity of access.
• If the only access to data is through methods you provide, you can ensure
consistency of access.
• Hides the implementation details from other objects.
• In large projects, a developer should not have to be aware of how a class is
structures.
• Simplifies code
• All access to the object through predefined interfaces.
Designing A Class
• A properly encapsulated class has the following traits.
• All data that belongs to the class is stored in the class.
• Or at least, represented directly in the class.
• All attributes are private.
• There’s almost never a good reason to make an attribute public.
• Hardly ever a good reason to make an attribute protected.
The Class Interface
• Classes have a defined interface.
• This is the set of public methods they expose.
• A properly encapsulated class has a coherent interface.
• It exposes only those methods that are of interest to external
classes.
• A properly encapsulated class has a clean divide between its
interface and its implementation.
Interfaces
• The easiest way to think of an interface is with a metaphor.
• Imagine your car’s engine.
• You don’t directly interact with the engine.
• You have an interface to that car engine that is defined by:
• Gears
• Pedals
• Ignition
• Likewise with the car wheels.
• Your steering wheel is the interface to your wheels.
• As long as the interface remains consistent…
• It doesn’t matter what engine is in the car.
• Change the interface, and you can no longer drive the car the same way.
Interface and Implementation
• Within a properly encapsulated class, it does not matter to
external developers how a class is implemented.
• I know what goes in
• I know what comes out
• The interface to a class can remain constant even while the
entirety of the class is rewritten.
• Not an uncommon act in development.
Interface and Implementation
• It’s important to design a clean an effective interface.
• It’s safe to change encapsulated implementation.
• It’s usually not safe to change a public interface.
• You are committed to the code that you expose publicly.
• When new versions of Java deprecate existing functionality, they
never just switch it off.
Interface
• Our interface may expose a set of public methods with a
certain signature.
• Return type
• Name
• Parameters
• As long as we keep those signatures constant, it doesn’t
matter how our class works internally.
Object Oriented Terminology
• Object orientation comes with an extra jargon burden.
• As with most things in programming.
• Variables in classes are usually known as attributes.
• It just means ‘a variable that belongs to a class’
• Functions are usually known as methods.
• Or behaviours.
Object Oriented Programs
• The relationship between objects and classes becomes very
complex in many computer programs.
• Some means of representing this complexity ‘at a glance’ is
useful.
• This is what a class diagram is used for.
• It gives the static view of a program’s architecture.
• Other techniques exist too.
• They vary in effectiveness.
Class Diagrams
• At their simplest, class diagrams are just boxes with lines
connecting them to other boxes.
• They show the relationship between classes only.
Class Diagrams
• At the next level of usefulness, class diagrams also contain
information about attributes and methods.
• Attributes in the second row
• Methods in the third
• At their best, class diagrams contain class relationships, methods,
attributes, and the visibility of each.
• They also explicitly show multiplicity.
• How many of one object relate to how many of another.
Class Diagram with Attributes
Summary
• Today we talked about encapsulation.
• Storing attributes along with the methods that act on them.
• It is a powerful technique for ensuring fidelity of our objects.
• People can’t change the except through the interfaces we provide.
• Good object design is to keep attributes private.
• And permit access only through accessor methods.

More Related Content

What's hot

Object Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsObject Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsEPAM
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 
OOP in C#
OOP in C#OOP in C#
OOP in C#DevMix
 
encapsulation
encapsulationencapsulation
encapsulationshalini392
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1Geophery sanga
 
Oo ps concepts in c++
Oo ps concepts in c++Oo ps concepts in c++
Oo ps concepts in c++Hemant Saini
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access SpecifiersMuhammad Hammad Waseem
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsAbhigyan Singh Yadav
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ FundamentalsSubhasis Nayak
 
Oop concepts classes_objects
Oop concepts classes_objectsOop concepts classes_objects
Oop concepts classes_objectsWilliam Olivier
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasShahzad Younas
 
Encapsulation and inheritance
Encapsulation and inheritanceEncapsulation and inheritance
Encapsulation and inheritanceChaudhary Kashif
 
27csharp
27csharp27csharp
27csharpSireesh K
 

What's hot (20)

Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
Object Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsObject Oriented Concepts in Real Projects
Object Oriented Concepts in Real Projects
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
OOP in C#
OOP in C#OOP in C#
OOP in C#
 
encapsulation
encapsulationencapsulation
encapsulation
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Ashish oot
Ashish ootAshish oot
Ashish oot
 
Oo ps concepts in c++
Oo ps concepts in c++Oo ps concepts in c++
Oo ps concepts in c++
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ Fundamentals
 
Oop concepts classes_objects
Oop concepts classes_objectsOop concepts classes_objects
Oop concepts classes_objects
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
Encapsulation and inheritance
Encapsulation and inheritanceEncapsulation and inheritance
Encapsulation and inheritance
 
General OOP Concepts
General OOP ConceptsGeneral OOP Concepts
General OOP Concepts
 
27csharp
27csharp27csharp
27csharp
 

Similar to CPP14 - Encapsulation

2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented ProgramMichael Heron
 
CPP16 - Object Design
CPP16 - Object DesignCPP16 - Object Design
CPP16 - Object DesignMichael Heron
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane2
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - SummationMichael Heron
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - RevisionMichael Heron
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - EncapsulationMichael Heron
 
OOM Unit I - III.pdf
OOM Unit I - III.pdfOOM Unit I - III.pdf
OOM Unit I - III.pdfShaikRafikhan1
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
OOP -interface and objects.pptx
OOP -interface and objects.pptxOOP -interface and objects.pptx
OOP -interface and objects.pptxEdFeranil
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesDr. Syed Hassan Amin
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentalsAlfred Jett Grandeza
 

Similar to CPP14 - Encapsulation (20)

2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program
 
CPP16 - Object Design
CPP16 - Object DesignCPP16 - Object Design
CPP16 - Object Design
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - Summation
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - Revision
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - Encapsulation
 
OOM Unit I - III.pdf
OOM Unit I - III.pdfOOM Unit I - III.pdf
OOM Unit I - III.pdf
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
OOP -interface and objects.pptx
OOP -interface and objects.pptxOOP -interface and objects.pptx
OOP -interface and objects.pptx
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
80410172053.pdf
80410172053.pdf80410172053.pdf
80410172053.pdf
 
Introduction
IntroductionIntroduction
Introduction
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 

More from Michael Heron

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interactionMichael Heron
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - InheritanceMichael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityMichael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - TexturesMichael Heron
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - ShadingMichael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationMichael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsMichael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsMichael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationMichael Heron
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - ModifiersMichael Heron
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IOMichael Heron
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - TemplatesMichael Heron
 

More from Michael Heron (20)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 

Recently uploaded

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp KrisztiĂĄn
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 

Recently uploaded (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 

CPP14 - Encapsulation

  • 2. Introduction • One of the things that distinguishes object orientation from other kinds of programming is the tight coupling between functions and data. • This is a principle known as encapsulation. • In this lecture we are going to talk about why this is such an important technique. • And the problems it was designed to resolve.
  • 3. Scope • Cast your minds back to a few weeks ago when we talked about global scope. • I mentioned this was an easy way to cause problems in large programs. • By storing data along with the methods that act on that data (in a class), it is possible to have access to data without global scope. • Variables inside a class have class-wide scope.
  • 4. Encapsulation • Encapsulation is the principle of keeping data and functions together in one unit. • The class • By doing this, we have a highly portable unit of data we can manipulate. • But only through the functions that we set. • We can set an interface on an object that permits us fine- grained control over access.
  • 5. Back To Our Account Class class Account { private: int balance; int overdraft; public: int query_balance(); void set_balance (int); int query_overdraft(); void set_overdraft (int); bool adjust_balance (int); };
  • 6. Private and Public • We set sections of the code to be either private or public. • private for variables • public for functions • These are known as visibility modifiers. • They change how visible these things are to other pieces of code. • There exists a third called protected. • Of more niche benefit.
  • 7. Private • Private visibility means that variables (or functions) are available only to the class in which they are defined. • We can access balance and overdraft as variables within the methods of our object. • We cannot access them outside of that object. • We do this to make sure that people cannot simply set the value on our data. • They have to go through methods we provide
  • 8. Public • Public means anything has access. • It’s the highest level of visibility. • We reserve this visibility for methods that we don’t mind everyone being able to use. • These methods make up our interface to the object. • We use a pair of such methods around private variables. • We call these access methods • Usually a query_x and a set_x.
  • 9. Impact of Change • It’s very common when writing a program to have to change the way things work. • Alas, this comes with major side-effects for many situations. • When working with other developers especially, you need to be wary. • The interface to the objects you write is a kind of informal contract with your colleagues. • It’s important to be respectful when developing.
  • 10. Impact of Change • Impact of Change defines how much code is likely to need changed if you make an adjustment to your code. • The higher the impact, the more code will need modified. • Some things are almost always safe to do. • Add in variables. • Add in methods • Some things are almost never safe to do. • Change what a method does
  • 11. Impact of Change • In large object oriented programs, there may be thousands of classes. • All interrelated in complex ways. • It is not possible to know which objects may be using public methods and variables in your code. • You have to assume if it can be accessed, it will be accessed. • Can’t change code indiscriminately. • That’s not respectful.
  • 12. Impact of Change • Instead, what we do is restrict visibility. • To private, ideally. • We expose as public only those methods we are happy to support. • This limits the impact of change. • We only need to change things in our own class if we modify things. • Important to be able to make adjustments in your code. • Private access permits this.
  • 13. What is the benefit? • Simplify documentation. • External documentation can focus on only relevant functions. • Can ensure fidelity of access. • If the only access to data is through methods you provide, you can ensure consistency of access. • Hides the implementation details from other objects. • In large projects, a developer should not have to be aware of how a class is structures. • Simplifies code • All access to the object through predefined interfaces.
  • 14. Designing A Class • A properly encapsulated class has the following traits. • All data that belongs to the class is stored in the class. • Or at least, represented directly in the class. • All attributes are private. • There’s almost never a good reason to make an attribute public. • Hardly ever a good reason to make an attribute protected.
  • 15. The Class Interface • Classes have a defined interface. • This is the set of public methods they expose. • A properly encapsulated class has a coherent interface. • It exposes only those methods that are of interest to external classes. • A properly encapsulated class has a clean divide between its interface and its implementation.
  • 16. Interfaces • The easiest way to think of an interface is with a metaphor. • Imagine your car’s engine. • You don’t directly interact with the engine. • You have an interface to that car engine that is defined by: • Gears • Pedals • Ignition • Likewise with the car wheels. • Your steering wheel is the interface to your wheels. • As long as the interface remains consistent… • It doesn’t matter what engine is in the car. • Change the interface, and you can no longer drive the car the same way.
  • 17. Interface and Implementation • Within a properly encapsulated class, it does not matter to external developers how a class is implemented. • I know what goes in • I know what comes out • The interface to a class can remain constant even while the entirety of the class is rewritten. • Not an uncommon act in development.
  • 18. Interface and Implementation • It’s important to design a clean an effective interface. • It’s safe to change encapsulated implementation. • It’s usually not safe to change a public interface. • You are committed to the code that you expose publicly. • When new versions of Java deprecate existing functionality, they never just switch it off.
  • 19. Interface • Our interface may expose a set of public methods with a certain signature. • Return type • Name • Parameters • As long as we keep those signatures constant, it doesn’t matter how our class works internally.
  • 20. Object Oriented Terminology • Object orientation comes with an extra jargon burden. • As with most things in programming. • Variables in classes are usually known as attributes. • It just means ‘a variable that belongs to a class’ • Functions are usually known as methods. • Or behaviours.
  • 21. Object Oriented Programs • The relationship between objects and classes becomes very complex in many computer programs. • Some means of representing this complexity ‘at a glance’ is useful. • This is what a class diagram is used for. • It gives the static view of a program’s architecture. • Other techniques exist too. • They vary in effectiveness.
  • 22. Class Diagrams • At their simplest, class diagrams are just boxes with lines connecting them to other boxes. • They show the relationship between classes only.
  • 23. Class Diagrams • At the next level of usefulness, class diagrams also contain information about attributes and methods. • Attributes in the second row • Methods in the third • At their best, class diagrams contain class relationships, methods, attributes, and the visibility of each. • They also explicitly show multiplicity. • How many of one object relate to how many of another.
  • 24. Class Diagram with Attributes
  • 25. Summary • Today we talked about encapsulation. • Storing attributes along with the methods that act on them. • It is a powerful technique for ensuring fidelity of our objects. • People can’t change the except through the interfaces we provide. • Good object design is to keep attributes private. • And permit access only through accessor methods.