1 / 13

Intro to CS – Honors I Information Hiding

Intro to CS – Honors I Information Hiding. Georgios Portokalidis gportoka@stevens.edu. Why Hide Information?. It is basically a way to avoid “information overload” A programmer who uses your method only needs to know what it is accomplishing not how

gyda
Download Presentation

Intro to CS – Honors I Information 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. Intro to CS – Honors IInformation Hiding Georgios Portokalidis gportoka@stevens.edu

  2. Why Hide Information? • It is basically a way to avoid “information overload” • A programmer who uses your method only needs to know what it is accomplishing not how • Another term for Information hiding is abstraction

  3. How to Describe a Method • Describing pre-conditions • A set of conditions that must be true before invoking a method • The method should not be used unless these conditions are true • The method can perform in unpredicted ways if the conditions were not met when it was invoked • Describing post-conditions • Post-conditions describe the effects the invocation of a method had • Use comments to describe pre- and post-conditions • /** • Precondition: years is a nonnegative number. • Postcondition: Returns the projected population of the receiving object • after the specified number of years. • */

  4. Hiding Instance Variables • We can control who can access a class’s instance variable by replacing the public keyword with private • The keyword private restricts access to only from within the class • Instance variables should be private • Prevent arbitrary operations on a class’s data • public class Dog • { • private String name; • private String breed; • private int age; • public void writeOutput() • { • System.out.println("Name: " + name); • System.out.println("Breed: " + breed); • System.out.println("Age in calendar years: " + age); • System.out.println("Age in human years: " + • getAgeInHumanYears()); • System.out.println(); • } • . . . Dog balto = new Dog(); balto.name = "Balto"; balto.age= 8; balto.breed= "Siberian Husky";

  5. Accessing Instance Variables • Design methods to • set the value of private instance variables • retrieve the value of private instance variables • public class Rectangle • { • private int width; • private int height; • private int area; • public void setDimensions(intnewWidth, intnewHeight) • { • width = newWidth; • height = newHeight; • area = width * height; • } • public intgetArea() • { • return area; • } • }

  6. What’s so Bad about Public Instance Variables • Class data could be corrupted • No longer possible to check pre-conditions • public class Rectangle • { • public intwidth; • public int height; • public int area; • private void calculateArea() • { • area = width * height; • } • public void setDimensions(intnewWidth, intnewHeight) • { • width = newWidth; • height = newHeight; • calculateArea(); • } • public intgetArea() • { • return area; • } • } Rectangle box = new Rectangle( ); box.setDimensions(10, 5); box.area = 30; System.out.println("The area of our rectangle is " + box.getArea());

  7. private Can Also be Applied to Methods • Private methods can be only invoked from another method in the class • Calling a method in the same class does not require a calling object • You can use this, as with class variables • Also serves to hide the how • public class Rectangle • { • private int width; • private int height; • private int area; • private void calculateArea() • { • area = width * height; • } • public void setDimensions(intnewWidth, intnewHeight) • { • width = newWidth; • height = newHeight; • calculateArea(); • } • public intgetArea() • { • return area; • } • } Omitting the keyword private is the same as using public. Always, use public or private to avoid confusion!

  8. Efficiency • Keep behavior the same • The what remains the same • Alter implementation • The how changes • public class Rectangle • { • private int width; • private int height; • private int area; • private void calculateArea() • { • area = width * height; • } • public void setDimensions(intnewWidth, intnewHeight) • { • width = newWidth; • height = newHeight; • } • public intgetArea() • { • return calculateArea(); • } • }

  9. Accessor and Mutator Methods • Instance variables should always be private • If access to instance variables need to be provided outside the class use method to access and/or mutate the variables • private intheight; • ... • public void setHeight(intnewHeight) • { • height = newHeight; • } • public intgetHeight() • { • return height; • }

  10. Revisiting Encapsulation • Interface: • Comments • Heading of public methods • Public named constants class definition • Implementation: • Private variables • Private methods • Private constants • Bodies of public methods User of the class A class’s API

  11. Javadoc • Everything between /** */ can be processed by Javadoc to automatically create API documentation • Tags (words prefixed with @) allow you to tag particular elements of your java program

  12. UML - indicates private Purchase - name: String - groupCount: int - groupPrice: double - numberBought: int + setName(String newName): void + setPrice(int count, double costForCount): void + setNumberBought(int number): void + readInput( ): void + writeOutput( ): void + getName( ): String + getTotalCost( ): double + getUnitCost( ): double + getNumberBought( ): int + indicates public

More Related