1 / 16

Data Hiding

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)

Télécharger la présentation

Data Hiding

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Hiding

  2. Public and Private Review • Let’s pretend that we are going to create an Alarm Clock class.

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

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

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

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

  7. The nice thing about Object Oriented Programming (OOP) is that it allows for: • 1. Encapsulation • 2. Data Hiding

  8. Data (Fields) ---------------------------------- Methods That Operate on the Data Object • 1. Encapsulation: • The combining of data and code into a single object.

  9. 2. Data Hiding • An object’s ability to hide its data from code that is outside the object. WHAT THE HECK DOES THAT MEAN?

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

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

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

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

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

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

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

More Related