SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Data/Information Hiding,
Encapsulation
Lecture 7
Asif Shahzad, Assistant Professor
COMSATS University Islamabad, Lahore Campus
Private Behavior and Attributes of object
• Our home is an object, its kitchen and bedroom are private. We do
not allow guests or strangers to visit our kitchen and bedrooms.
• In same way, mobile number is our attribute, but we do not share it
with everyone. It’s our private attribute.
• We do not allow others access or call our certain behavior e.g.
shopping and eating.
• In same way, an automatic car do not allow the driver to change its
gear, it changes it automatically.
Use Access Modifiers: public or private
• We can make certain behavior private, by declaring it with
‘private’ access modifier. Example, hide calculateAverage()
• In same way, to make an attribute hidden from outside
world, declare it as private e.g. mobileNumber.
• Other classes, where object of our class is created, would not
be able to read/call private attributes/methods. Private
attributes and methods are accessible within the class.
Lec07   data hiding and encapsulation
Encapsulation
1. Lets say, we want Student id be a positive integer. How to do
it? Demo..
2. So, to encapsulate a field, we can make it private and create
public get and set methods to give read/write access of
attribute to others.
3. In object oriented design, its Student class responsibility to
make sure, it will always store a valid id.
4. So encapsulation help to prevent data corruption by other
objects / entities.
Why we declare an attribute as private?
• Why we declare an attribute as private?
• To ensure data validity
• To enforce some organization constraints at write and read opration
• To have some private state which other objects has no concern
Conventions About get/set methods
• Method name is prefixed with ‘get’ or ‘set’ followed by attribute
name (use camel notation for methods).
• The get method's return type must be the type of attribute it returns
and it must not receive any argument.
• The set method's return type is always void and it must receive only
one argument whose type must be same as the type of the attribute
being set.
• Access modifier for the get and set methods is generally public, but its
not a requirement or convention. It can be changed as per application
requirements.
Next !
How we can initialize state at the
time of object construction?
Constructors

More Related Content

PPTX
Friend function
PPTX
Data types IN JAVA
PPTX
PDF
12. Objects I
DOCX
C# concepts
PPTX
PPT
Jdt Features
Friend function
Data types IN JAVA
12. Objects I
C# concepts
Jdt Features

Similar to Lec07 data hiding and encapsulation (20)

PPTX
Module 4 Effect of Reuse on using Encapsulation.pptx
PPTX
004_Private and public attributes and methods.pptx
PPTX
Lecture_7-Encapsulation in Java.pptx
PPTX
SAD05 - Encapsulation
PPTX
ENCAPSULATION module for IT or comsci.pptx
PPTX
2 Object-oriented programghgrtrdwwe.pptx
PPTX
Java Chapter 04 - Writing Classes: part 3
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
PPTX
OOP02-27022023-090456am.pptx
PPTX
CPP14 - Encapsulation
PPTX
Access modifiers in java
PDF
Lecture09a computer applicationsie1_dratifshahzad
PPTX
Object Oriented Programming Week 2 Lecture
PPT
C++Presentation 2.PPT
PPTX
2CPP09 - Encapsulation
PPTX
Encapsulation
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
Module 4 Effect of Reuse on using Encapsulation.pptx
004_Private and public attributes and methods.pptx
Lecture_7-Encapsulation in Java.pptx
SAD05 - Encapsulation
ENCAPSULATION module for IT or comsci.pptx
2 Object-oriented programghgrtrdwwe.pptx
Java Chapter 04 - Writing Classes: part 3
[OOP - Lec 08] Encapsulation (Information Hiding)
OOP02-27022023-090456am.pptx
CPP14 - Encapsulation
Access modifiers in java
Lecture09a computer applicationsie1_dratifshahzad
Object Oriented Programming Week 2 Lecture
C++Presentation 2.PPT
2CPP09 - Encapsulation
Encapsulation
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Ad

More from Asif Shahzad (10)

PPTX
Cascading Style Sheets CSS
PPTX
Lec03 print on console and getting input
PPTX
Lec02 primitive types
PPTX
Lec01 intro and hello world program
PPTX
Lec08 constructors
PPTX
Lec04 if-else and loops
PPTX
Lec05 class and object
PPTX
Cv writing and job search
PPTX
Domain Name System (DNS) - Domain Registration and Website Hosting Basics
PPTX
Scope of Information Technology
Cascading Style Sheets CSS
Lec03 print on console and getting input
Lec02 primitive types
Lec01 intro and hello world program
Lec08 constructors
Lec04 if-else and loops
Lec05 class and object
Cv writing and job search
Domain Name System (DNS) - Domain Registration and Website Hosting Basics
Scope of Information Technology
Ad

Recently uploaded (20)

DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PPTX
Introduction to Windows Operating System
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PDF
MCP Security Tutorial - Beginner to Advanced
PDF
Multiverse AI Review 2025: Access All TOP AI Model-Versions!
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PDF
Topaz Photo AI Crack New Download (Latest 2025)
PDF
Salesforce Agentforce AI Implementation.pdf
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
Trending Python Topics for Data Visualization in 2025
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PDF
Cost to Outsource Software Development in 2025
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PPTX
Patient Appointment Booking in Odoo with online payment
PPTX
Cybersecurity: Protecting the Digital World
PDF
Time Tracking Features That Teams and Organizations Actually Need
How to Use SharePoint as an ISO-Compliant Document Management System
Introduction to Windows Operating System
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
MCP Security Tutorial - Beginner to Advanced
Multiverse AI Review 2025: Access All TOP AI Model-Versions!
Computer Software and OS of computer science of grade 11.pptx
Wondershare Recoverit Full Crack New Version (Latest 2025)
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
Topaz Photo AI Crack New Download (Latest 2025)
Salesforce Agentforce AI Implementation.pdf
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Trending Python Topics for Data Visualization in 2025
Designing Intelligence for the Shop Floor.pdf
DNT Brochure 2025 – ISV Solutions @ D365
Cost to Outsource Software Development in 2025
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Patient Appointment Booking in Odoo with online payment
Cybersecurity: Protecting the Digital World
Time Tracking Features That Teams and Organizations Actually Need

Lec07 data hiding and encapsulation

  • 1. Data/Information Hiding, Encapsulation Lecture 7 Asif Shahzad, Assistant Professor COMSATS University Islamabad, Lahore Campus
  • 2. Private Behavior and Attributes of object • Our home is an object, its kitchen and bedroom are private. We do not allow guests or strangers to visit our kitchen and bedrooms. • In same way, mobile number is our attribute, but we do not share it with everyone. It’s our private attribute. • We do not allow others access or call our certain behavior e.g. shopping and eating. • In same way, an automatic car do not allow the driver to change its gear, it changes it automatically.
  • 3. Use Access Modifiers: public or private • We can make certain behavior private, by declaring it with ‘private’ access modifier. Example, hide calculateAverage() • In same way, to make an attribute hidden from outside world, declare it as private e.g. mobileNumber. • Other classes, where object of our class is created, would not be able to read/call private attributes/methods. Private attributes and methods are accessible within the class.
  • 5. Encapsulation 1. Lets say, we want Student id be a positive integer. How to do it? Demo.. 2. So, to encapsulate a field, we can make it private and create public get and set methods to give read/write access of attribute to others. 3. In object oriented design, its Student class responsibility to make sure, it will always store a valid id. 4. So encapsulation help to prevent data corruption by other objects / entities.
  • 6. Why we declare an attribute as private? • Why we declare an attribute as private? • To ensure data validity • To enforce some organization constraints at write and read opration • To have some private state which other objects has no concern
  • 7. Conventions About get/set methods • Method name is prefixed with ‘get’ or ‘set’ followed by attribute name (use camel notation for methods). • The get method's return type must be the type of attribute it returns and it must not receive any argument. • The set method's return type is always void and it must receive only one argument whose type must be same as the type of the attribute being set. • Access modifier for the get and set methods is generally public, but its not a requirement or convention. It can be changed as per application requirements.
  • 8. Next ! How we can initialize state at the time of object construction? Constructors