data hiding

16
Data Hiding

Upload: lani-vaughan

Post on 31-Dec-2015

23 views

Category:

Documents


2 download

DESCRIPTION

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) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Hiding

Data Hiding

Page 2: Data Hiding

Public and Private Review

• Let’s pretend that we are going to create an Alarm Clock class.

Page 3: Data Hiding

• What would the Alarm Clock class’ data fields be?

Public and Private Review

• 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

Page 4: Data Hiding

• What would the Alarm Clock class’ PUBLIC methods be?

Public and Private Review

• Methods:– Set Hour– Set Minute– Set Meridiem– Set Alarm

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

Page 5: Data Hiding

• What would the Alarm Clock class’ PRIVATE methods be?

Public and Private Review

• 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.

Page 6: Data Hiding

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.

Page 7: Data Hiding

• The nice thing about Object Oriented Programming (OOP) is that it allows for:

– 1. Encapsulation

– 2. Data Hiding

Page 8: Data Hiding

• 1. Encapsulation:– The combining of data and code into a single

object.Data (Fields)

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

Methods That Operate on the Data

Object

Page 9: Data Hiding

• 2. Data Hiding– An object’s ability to hide its data from code

that is outside the object.

WHAT THE HECK DOES THAT MEAN?

Page 10: Data Hiding

Data Hiding

Data (Fields)

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

Methods That Operate on the Data

Object of Main Class

Data (Fields)

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

public

getAge( )

Object of Dog Class

public

setAge( )

private

calcDogYears( )

Methods

private intDogYears = 24

private intAge = 4

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

This is like our demo that we did.

Page 11: Data Hiding

Data Hiding

Data (Fields)

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

Methods That Operate on the Data

Object of Main Class

Data (Fields)

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

public

getAge( )

Object of Dog Class

public

setAge( )

private

calcDogYears( )

Methods

private intDogYears = 24

private intAge = 4

These Dog class variables have been made private.

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

are “hidden.”

Page 12: Data Hiding

Data Hiding

Data (Fields)

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

Methods That Operate on the Data

Object of Main Class

?So how can the

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

Data (Fields)

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

public

getAge( )

Object of Dog Class

public

setAge( )

private

calcDogYears( )

Methods

private intDogYears = 24

private intAge = 4

Page 13: Data Hiding

Data Hiding

Data (Fields)

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

Methods That Operate on the Data

Object of Main Class

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

getAge( )

Object of Dog Class

public

setAge( )

private

calcDogYears( )

Methods

public intDogYears = 24

public intAge = 4

Page 14: Data Hiding

Data Hiding

Data (Fields)

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

Methods That Operate on the Data

Object of Main Class

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

getAge( )

Object of Dog Class

public

setAge( )

private

calcDogYears( )

Methods

public intDogYears = 1000

public intAge = 2

Page 15: Data Hiding

Data Hiding

Data (Fields)

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

Methods That Operate on the Data

Object of Main Class

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)

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

Object of Dog Class

public

setAge( )

private

calcDogYears( )

Methods

priavte intDogYears = 1000

private intAge = 2

public

getAge( )Data Hiding is important because…

Page 16: Data Hiding

• 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.