• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
12   encapsulation
 

12 encapsulation

on

  • 383 views

 

Statistics

Views

Total Views
383
Views on SlideShare
383
Embed Views
0

Actions

Likes
0
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    12   encapsulation 12 encapsulation Presentation Transcript

    • 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