Encapsulation - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Encapsulation

Description:

to enclose in or as if in a capsule. Computing. the process of combining elements to create a new entity. ... encapsulation is the inclusion within a program ... – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 13
Provided by: davidmutch8
Category:

less

Transcript and Presenter's Notes

Title: Encapsulation


1
Encapsulation
  • English definition
  • to enclose in or as if in a capsule
  • Computing
  • the process of combining elements to create a new
    entity.
  • encapsulation is the inclusion within a program
    object of all the resources need for the object
    to function
  • Methods
  • Fields
  • the object is accessed through its interfaces
  • the idea is I will tell you only what I want and
    do what I want

2
Motivation
3
Motivation
  • Encapsulation is the American Way
  • I respect your privacy of your data
  • I will trust you to do what you tell me you will
    do
  • Encapsulation a religious commandment
  • Thou shalt encapsulate thine objects

4
What is Wrong With This Picture?
  • public class myClass
  • private int i
  • public int j

5
What is Wrong With This Picture?
  • public class myClass
  • private int i
  • public int j
  • What can someone do to j?

6
What is Wrong With This Picture?
Start of method
  • public void setUpButtons()
  • buttonPanel new JPanel()
  • c.add( buttonPanel , BorderLayout.CENTER )
  • bZero new JButton( "0" )
  • bZero.setFont( new Font("Sanserif",
    Font.BOLD, 16 ) )
  • bOne new JButton( "1" )
  • bTwo new JButton( "2" )
  • bThree new JButton( "3" )
  • bFour new JButton( "4" )
  • bFive new JButton( "5" )
  • bSix new JButton( "6" )
  • bSeven new JButton( "7" )
  • bEight new JButton( "8" )
  • bNine new JButton( "9" )
  • bExit new JButton( "Exit" )
  • bClear new JButton( "Clear" )
  • bClear.setBackground( Color.blue )
  • bClear.setForeground( Color.white )
  • bMult new JButton( "" )

bDecPt new JButton( "." )
bDecPt.setFont( new Font("Sanserif", Font.BOLD,
22 ) ) / add the following buttons to
the buttonPanel panel. / buttonPanel.add(
bSeven ) buttonPanel.add( bEight )
buttonPanel.add( bNine )
buttonPanel.add( bFour ) buttonPanel.add(
bFive ) buttonPanel.add( bSix )
buttonPanel.add( bOne ) buttonPanel.add(
bTwo ) buttonPanel.add( bThree )
buttonPanel.add( bZero ) buttonPanel.add(
bDecPt ) buttonPanel.add( bEquals )
buttonPanel.setLayout( new GridLayout( 5, 3, 5,
5 ) ) bDecPt.addActionListener( this )
bZero.addActionListener( this )
bOne.addActionListener( this )
bTwo.addActionListener( this )
bThree.addActionListener( this )
bFour.addActionListener( this )
bFive.addActionListener( this )
bSix.addActionListener( this )
bSeven.addActionListener( this )
bEight.addActionListener( this )
bNine.addActionListener( this )
bExit.addActionListener( this )
bMult.addActionListener( this )
bDiv.addActionListener( this )
bSub.addActionListener( this )
bPlus.addActionListener( this )
bEquals.addActionListener( this )
bClear.addActionListener( this )
End of method
7
What Does Encapsulation Do?
  • Forces classes to be independent and hide the
    intricate details from the other classes
  • All changes made to the object are made by itself
    (through its methods)
  • Encapsulation dictates that
  • all the attributes must be private
  • all the attributes can be accessed only through
    its methods

8
Why Encapsulate?
  • Modular programs
  • every class is self-contained
  • Code reuse
  • classes can reused in other programs
    (plug-n-play)
  • Improves code maintainability
  • programs can be modified independently
  • Enables concurrent development
  • developers program to interfaces

9
Why Encapsulate?
  • If you still dont believe in encapsulating, do
    it because I told you so!

10
Arrogance!
11
Side-effect of Arrogance!
12
Encapsulation Violations
  • Public attributes that other classes can access
    and modify
Write a Comment
User Comments (0)
About PowerShow.com