SlideShare une entreprise Scribd logo
1  sur  5
Encapsulation DhrubojyotiKayal
The attributes (data) and the behaviours (code) are encapsulated in to a single entity/container or class Do not reveal the internals (fields) of the container to the external world External world can use and change the internals using the exposed handles or behavior or methods What is encapsulation?
Data Hiding Essential to ensure that the state is not altered accidentally without applying the applicable constraints and rules Make the data members private provide getters and setters Encapsulation in Java
Create a BankAccount class with a public double field balance with initial value of 100 Use a BankAccountTest class and from main method access this field to show the balance Now write a new method withdraw which reduces balance by input amount Now set the balance to 0 Withdraw 50 ? Now you have negative balance? Change the class and apply rules to ensure no negative balance. Use getters and setters Exercise
Q&A

Contenu connexe

Similaire à 12 encapsulation

Coding Component (50)Weve provided you with an implementation .docx
Coding Component (50)Weve provided you with an implementation .docxCoding Component (50)Weve provided you with an implementation .docx
Coding Component (50)Weve provided you with an implementation .docxmary772
 
Form personalization
Form personalization Form personalization
Form personalization nikhilgla
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#Muhammad Younis
 
Cis407 a ilab 6 web application development devry university
Cis407 a ilab 6 web application development devry universityCis407 a ilab 6 web application development devry university
Cis407 a ilab 6 web application development devry universitylhkslkdh89009
 
need help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docxneed help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docxniraj57
 
Workload Management in SQL Server 2019
Workload Management in SQL Server 2019Workload Management in SQL Server 2019
Workload Management in SQL Server 2019Antonios Chatzipavlis
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexSalesforce Developers
 
Form personalization 395117_r12_updated1212
Form personalization 395117_r12_updated1212Form personalization 395117_r12_updated1212
Form personalization 395117_r12_updated1212flower705
 
CaseStudy-MohammedImranAlam-Xcelsius
CaseStudy-MohammedImranAlam-XcelsiusCaseStudy-MohammedImranAlam-Xcelsius
CaseStudy-MohammedImranAlam-XcelsiusMohammed Imran Alam
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexSalesforce Developers
 
Week 2 Individual Assignment 2 Quantitative Analysis of Credit - .docx
Week 2 Individual Assignment 2 Quantitative Analysis of Credit - .docxWeek 2 Individual Assignment 2 Quantitative Analysis of Credit - .docx
Week 2 Individual Assignment 2 Quantitative Analysis of Credit - .docxjessiehampson
 
2580 microsoft excel2010_rtm_wsg_external
2580 microsoft excel2010_rtm_wsg_external2580 microsoft excel2010_rtm_wsg_external
2580 microsoft excel2010_rtm_wsg_externalBrittanyShatia
 
Structured Query Language for Data Management 2 Sructu.docx
Structured Query Language for Data Management      2 Sructu.docxStructured Query Language for Data Management      2 Sructu.docx
Structured Query Language for Data Management 2 Sructu.docxjohniemcm5zt
 

Similaire à 12 encapsulation (20)

Stored procedures
Stored proceduresStored procedures
Stored procedures
 
inheritance and polymorphism
inheritance and polymorphisminheritance and polymorphism
inheritance and polymorphism
 
Coding Component (50)Weve provided you with an implementation .docx
Coding Component (50)Weve provided you with an implementation .docxCoding Component (50)Weve provided you with an implementation .docx
Coding Component (50)Weve provided you with an implementation .docx
 
31 days Refactoring
31 days Refactoring31 days Refactoring
31 days Refactoring
 
Form personalization
Form personalization Form personalization
Form personalization
 
Df12 Performance Tuning
Df12 Performance TuningDf12 Performance Tuning
Df12 Performance Tuning
 
Code Testability
Code TestabilityCode Testability
Code Testability
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
 
Cis407 a ilab 6 web application development devry university
Cis407 a ilab 6 web application development devry universityCis407 a ilab 6 web application development devry university
Cis407 a ilab 6 web application development devry university
 
need help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docxneed help completing week 6 ilab.. i will upload what I currently ha.docx
need help completing week 6 ilab.. i will upload what I currently ha.docx
 
Workload Management in SQL Server 2019
Workload Management in SQL Server 2019Workload Management in SQL Server 2019
Workload Management in SQL Server 2019
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
 
Form personalization 395117_r12_updated1212
Form personalization 395117_r12_updated1212Form personalization 395117_r12_updated1212
Form personalization 395117_r12_updated1212
 
CaseStudy-MohammedImranAlam-Xcelsius
CaseStudy-MohammedImranAlam-XcelsiusCaseStudy-MohammedImranAlam-Xcelsius
CaseStudy-MohammedImranAlam-Xcelsius
 
Icom4015 lecture7-f16
Icom4015 lecture7-f16Icom4015 lecture7-f16
Icom4015 lecture7-f16
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
 
Week 2 Individual Assignment 2 Quantitative Analysis of Credit - .docx
Week 2 Individual Assignment 2 Quantitative Analysis of Credit - .docxWeek 2 Individual Assignment 2 Quantitative Analysis of Credit - .docx
Week 2 Individual Assignment 2 Quantitative Analysis of Credit - .docx
 
2580 microsoft excel2010_rtm_wsg_external
2580 microsoft excel2010_rtm_wsg_external2580 microsoft excel2010_rtm_wsg_external
2580 microsoft excel2010_rtm_wsg_external
 
Oracle SQL Training in Chennai, Tambaram
Oracle SQL Training in Chennai, TambaramOracle SQL Training in Chennai, Tambaram
Oracle SQL Training in Chennai, Tambaram
 
Structured Query Language for Data Management 2 Sructu.docx
Structured Query Language for Data Management      2 Sructu.docxStructured Query Language for Data Management      2 Sructu.docx
Structured Query Language for Data Management 2 Sructu.docx
 

Plus de dhrubo kayal

01 session tracking
01   session tracking01   session tracking
01 session trackingdhrubo kayal
 
03 handling requests
03 handling requests03 handling requests
03 handling requestsdhrubo kayal
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servletsdhrubo kayal
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 
08 class and object
08   class and object08   class and object
08 class and objectdhrubo kayal
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variablesdhrubo kayal
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with javadhrubo kayal
 

Plus de dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
11 static
11   static11   static
11 static
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
08 class and object
08   class and object08   class and object
08 class and object
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 

12 encapsulation

  • 2. The attributes (data) and the behaviours (code) are encapsulated in to a single entity/container or class Do not reveal the internals (fields) of the container to the external world External world can use and change the internals using the exposed handles or behavior or methods What is encapsulation?
  • 3. Data Hiding Essential to ensure that the state is not altered accidentally without applying the applicable constraints and rules Make the data members private provide getters and setters Encapsulation in Java
  • 4. Create a BankAccount class with a public double field balance with initial value of 100 Use a BankAccountTest class and from main method access this field to show the balance Now write a new method withdraw which reduces balance by input amount Now set the balance to 0 Withdraw 50 ? Now you have negative balance? Change the class and apply rules to ensure no negative balance. Use getters and setters Exercise
  • 5. Q&A