data hiding n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Data Hiding PowerPoint Presentation
Download Presentation
Data Hiding

Loading in 2 Seconds...

play fullscreen
1 / 16

Data Hiding - PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on

Data Hiding. Public and Private Review. Let’s pretend that we are going to create an Alarm Clock class. Public and Private Review. What would the Alarm Clock class’ data fields be?. Data Fields: Current Second (between 0 and 59) Current Minute (between 0 and 59)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Data Hiding' - lani-vaughan


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
public and private review
Public and Private Review
  • Let’s pretend that we are going to create an Alarm Clock class.
public and private review1
Public and Private Review
  • What would the Alarm Clock class’ data fields be?
  • Data Fields:
    • Current Second (between 0 and 59)
    • Current Minute (between 0 and 59)
    • Current Hour (between 0 and 59)
    • Meridiem Status: (PM or AM)
    • Alarm Time: (Hour and Minute)
    • Alarm Setting: On or Off
public and private review2
Public and Private Review
  • What would the Alarm Clock class’ PUBLIC methods be?
  • Methods:
    • Set Hour
    • Set Minute
    • Set Meridiem
    • Set Alarm

These methods are PUBLIC, which means that YOU have access to them.

public and private review3
Public and Private Review
  • What would the Alarm Clock class’ PRIVATE methods be?
  • Private Methods:
    • Increment Current Second
    • Increment Current Minute
    • Increment Current Hour
    • Change Meridiem Status
    • Sound Alarm

These methods are PRIVATE, which means that you DO NOT have access to them. Only the object has access to them.

public and private review4
Public and Private Review
  • Public Variables and Methods:
    • Variables and methods(MEMBERS) that are public to all code – including code outside the class.
  • Private Variables and Methods:
    • Variables and methods(MEMBERS) that are only accessible by code inside the class.
slide7
The nice thing about Object Oriented Programming (OOP) is that it allows for:
    • 1. Encapsulation
    • 2. Data Hiding
slide8

Data (Fields)

----------------------------------

Methods That Operate on the Data

Object

  • 1. Encapsulation:
    • The combining of data and code into a single object.
slide9
2. Data Hiding
    • An object’s ability to hide its data from code that is outside the object.

WHAT THE HECK DOES THAT MEAN?

data hiding1
Data Hiding

Let’s look at this diagram for a second so we can understand it.

This is like our demo that we did.

Data (Fields)

private intDogYears = 24

private intAge = 4

Data (Fields)

----------------------------------

--------------------

public

getAge( )

public

setAge( )

private

calcDogYears( )

Methods That Operate on the Data

Methods

Object of Dog Class

Object of Main Class

data hiding2
Data Hiding

These Dog class variables have been made private.

Data (Fields)

private intDogYears = 24

private intAge = 4

Data (Fields)

----------------------------------

--------------------

This means that other classes, like the Main class, cannot access those variables directly. The Dog class variables are “hidden.”

public

getAge( )

public

setAge( )

private

calcDogYears( )

Methods That Operate on the Data

Methods

Object of Dog Class

Object of Main Class

data hiding3
Data Hiding

Data (Fields)

?

So how can the Main class get the information from those variables in the Dog Class?

private intDogYears = 24

private intAge = 4

----------------------------------

Data (Fields)

--------------------

public

getAge( )

public

setAge( )

private

calcDogYears( )

Methods That Operate on the Data

Methods

Object of Dog Class

Object of Main Class

data hiding4
Data Hiding

You could make the Data fields public, But then those variables would not be hidden from pranksters and teenage java students.

For example, you don’t want anyone to have direct access to the intDogYears variable. You only want that updated if the intAge variable is updated.

Data (Fields)

public intDogYears = 24

public intAge = 4

Data (Fields)

----------------------------------

--------------------

public

getAge( )

public

setAge( )

private

calcDogYears( )

Methods That Operate on the Data

Methods

Object of Dog Class

Object of Main Class

data hiding5
Data Hiding

If intDogYears was public, someone could write code to update that variable regardless if intAge had been updated.

Someone could set intDogYears to 1000 regardless of what intAge was. You could have the following:

intAge = 2

intDogYears = 1000

That info is wrong!

Data (Fields)

public intDogYears = 1000

public intAge = 2

Data (Fields)

----------------------------------

--------------------

public

getAge( )

public

setAge( )

private

calcDogYears( )

Methods That Operate on the Data

Methods

Object of Dog Class

Object of Main Class

data hiding6
Data Hiding

It is best that intDogYears remain hidden. To do this you make the variable private.

You also create a private method that accesses intDogYears only when the setAge( ) method is called.

Data (Fields)

priavte intDogYears = 1000

private intAge = 2

Data (Fields)

----------------------------------

--------------------

public

getAge( )

public

setAge( )

private

calcDogYears( )

Data Hiding is important because…

Methods That Operate on the Data

Methods

Object of Dog Class

Object of Main Class

slide16
Data Hiding Is Important Because:
    • 1. The programmer that creates the Dog class controls HOW the Dog class’s methods will access the Dog class variables.
    • 2. Any other program (like the Main class) that wants to get the information from the Dog class’s variables HAS to use the Dog class’s public methods. This provides stability of code.