Presentation is loading. Please wait.

Presentation is loading. Please wait.

Information Hiding and Encapsulation

Similar presentations


Presentation on theme: "Information Hiding and Encapsulation"— Presentation transcript:

1 Information Hiding and Encapsulation

2 Information Hiding A programmer using a method that you have defined does not need to know the details of the code in the body of the method. This simplifies the work of the programmer using your method(s). Designing a method so that it can be used without any need to understand the detail of the code is called information hiding.

3 Precondition and Postcondition Comments
The precondition for a method states the conditions that must be true before the method is invoked. The postcondition describes the effect of the method invocation. It tells what will be true after the method is executed assuming that the precondition is true. For a method that returns a value, the postcondition describes the value returned. For a void method, the postcondition describes changes to the calling object

4 Example of Precondition and Postcondition Comments
/** Precondition: The instance variables of the calling object have values. Postcondition: the data stored in (the instance variables of) the calling object have been written to the screen. */ public void writeOutput()

5 Another Example of Precondition and Postcondition Comments
/** Precondition: years is a nonnegative number. Postcondition: Returns the projected population of the calling object after the specified number of years. */ public int projectedPopulation(int years)

6 Assertion Checks An assertion is something that says something about the state of your program. An assertion can be either true or false. If there are no mistakes in your program, it should be true. Preconditions and postconditions are examples of assertions.

7 Assertion Checks (cont’d)
You can have assertion comments at other places in your code besides the beginning or the end. For example all the comments in the following code are assertions. //n == 1 while (n < limit) { n = 2*n; } //n >= limit //n is the smallest power of 2 >= limit.

8 Assertion Checks (cont’d)
In Java, instead of just inserting comments about assertions, you can insert an assertion check statement that will stop the program if the condition is false. Such a statement has the form: assert Boolean Expression; If you compile and run your program in the proper way, the assertion check statement will stop your program if the condition is false. If it is true nothing happens.

9 Assertion Checks (cont’d)
We can replace two of the comments in the previous example with assertion statements. assert n == 1; while (n < limit) { n = 2*n; } assert n >= limit; //n is the smallest power of 2 >= limit.

10 Assertion Checks (cont’d)
The comment: //n is the smallest power of 2 >= limit. cannot easily be replaced with an assertion statement; so we leave it as a comment. In order for assertion statements to be recognized and executed, your program must be compiled using the following command line statement: javac –source 1.5 YourProgram.java

11 Assertion Checks (cont’d)
To run the program with assertion checking turned on, enter the following command: java –enableassertions YourProgram If you run your program in the normal way, assertion checking is turned off, and all assert statements are ignored.

12 The Public and Private Modifiers
It is considered good programming practice to make all instance variables private. Whenever you place the modifier private before an instance variable, that instance variable’s name is not accessible outside of the class definition. Only methods within the class can access instance variables that are declared to be private.

13 The Public and Private Modifiers (cont’d)
This does not mean that you cannot change the values of private instance variables from outside the class where they are declared. It only means that you must invoke a method from within the class to set or change values of instance variables. This means, of course, that the class must have methods to accomplish this.

14 The Public and Private Modifiers (cont’d)
In the SpeciesThirdTry class example, readInput is an example of such a method. SpeciesThirdTry xSpecies = new SpeciesThirdTry(); //Valid xSpecies.readInput(); //Valid xSpecies.name = “Aardvark”; //Invalid System.out.println(xSpecies.population); //Invalid System.out.println(xSpecies.growthRate); //Invalid Methods can also be private, so methods like readInput must be public.

15 Accessor and Mutator Methods
All instance variables should be private, and accessor and mutator methods should be provided for accessing and setting or changing values of instance variables from outside their class. A public method that reads and returns data from one or more private instance variables is called an accessor method.

16 Accessor and Mutator Methods (cont’d)
Names of accessor methods typically begin with get. A public method that changes the data stored in one or more private instance variables is called a mutator method. Names of mutator methods typically begin with set.

17 Example of a Mutator Method
public void set(String newName, int newPopulation, double newGrowthRate) { name = newName; if (newPopulation >= 0) population = newPopulation; else System.out.println( “ERROR: using a negative population.”); System.exit(0); } growthRate = newGrowthRate;

18 Example of Accessor Methods
public String getName(); { return name; } public String getPopulation(); return population; public String getGrowthRate(); return growthRate;

19 Encapsulation This is the process of hiding all of the details of a class definition to make using the class easier. When done correctly, a class can be divided into two parts, the user interface and the implementation. The implementation consists of all the private instance variables of a class and the definitions of all the methods, both public and private.

20 Encapsulation (cont’d)
The user interface consists of all the headings for the public methods and defined constants of the class, along with comments that tell a programmer how to use these public methods and public defined constants. When a class is defined in a way that conceptually separates the user interface and the implementation we say that the class is well encapsulated.

21 Guidelines for Defining a Well-Encapsulated Class
Place a comment before the class definition that describes how the programmer should think about the class data and methods. Declare all instance variables in the class to be private. Provide public accessor and mutator methods to read and change data in an object of the class and any other methods a programmer may need to manipulate data in the class, e.g., input and output methods.

22 Guidelines for Defining a Well-Encapsulated Class (cont’d)
Fully specify how to use each public method with a comment placed before the method heading. Make any helping methods (methods to be used only by other methods of the class) private. Place all user interface comments (those that describe how to use the class or method of the class) before the class definition and before public method definitions. A general rule is to use /* */ comments for user interface comments and // comments for implementation comments.

23 Automatic Documentation with javadoc
javadoc is a tool that automatically generates user interface documentation for your classes. javadoc extracts comments and other information from your classes to provide the information necessary for someone to use your program, but the usefulness of the documentation depends, in part, on the usefulness of the comments you place in your code.

24 UML Class Diagrams Class diagrams are normally created before the class is defined. They provide a guide to the programmer in implementing the code necessary to solve the problem at hand. A minus sign before an instance variable or method indicated that it is private. A plus sign means that it is public.

25 UML Class Diagram Example
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


Download ppt "Information Hiding and Encapsulation"

Similar presentations


Ads by Google