130 likes | 270 Views
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
E N D
Intro to CS – Honors IInformation 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 • Another term for Information hiding is abstraction
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. • */
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";
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; • } • }
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());
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!
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(); • } • }
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; • }
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
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
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