12 encapsulation

466 views
431 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
466
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

12 encapsulation

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

×