Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

69
Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Transcript of Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Page 1: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 1

Encapsulation

© 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Page 2: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Objectives

● Software development process● Encapsulation● Terminology and concepts related to objects● Expands on chapter subjects

Page 3: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● Essential that programs be correct.– Contains no bugs

● Efficient– No more time or memory than necessary

● General-purpose– Ability to build upon program for future use.

● Rapidly developed

Page 4: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● Tradeoffs in a computer program

Page 5: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● In some applications, such as medical equipments, aircraft navigation, and nuclear power plant control, lives may literally depend on correctness of software.– To ensure correctness, thoroughly test (takes too

much time).– Practical concerns (to release quickly) often lead

to the release of buggy software.

Page 6: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development● Data structure

– A way of organizing information– By Linux: A data structure is a way of storing information

in a computer so that it can be used efficiently. – By SQL Server: A data structure is a specialized format for

organizing and storing data.

● Algorithm– A logical sequence for solving a problem.– Step-by-step process for doing something– By Whatis.com: An algorithm (pronounced AL-go-rith-um)

is a procedure or formula for solving a problem.

● Niclaus Wirth: Data Structures + Algorithms = Programs

Page 7: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● To make the best choices, we would like to know as much as possible about how our program will be used.– What kinds of data– Which operations will be most common

Page 8: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● If a program is to be used very heavily be sure to optimize.– We can save development time by reusing code.– Try to create general-purpose components

● Example: write a method which can sort an array of any length, containing any values of any comparable type (numbers, letters, strings, and so on).

– This general-purpose code tends to be less efficient than code written for a specific use.

– Also once it is written and thoroughly documented we never need to think about its inner workings again.

– Java has huge, general purpose libraries.

Page 9: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● Most development time is spent on debugging.– Reduce debugging time by investing time in design

and testing.– Hastily (hurriedly) thrown (put) together programs

can become difficult to maintain.

Page 10: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● Encapsulation– The division of a program into distinct

components which have limited interaction.● Polymorphism (poly: multi; morphism: forms)

– The use of the same word or symbol to mean different things in different contexts.

● Inheritance– The ability to specify that a program is similar to

another program, delineating (describing) only the differences.

Page 11: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● Encapsulation makes it easier to rapidly develop correct programs, because a programmer only has to consider a few things when writing any one component of the program.

● Information hiding– The workings of a component should not be

visible from the outside.● Software engineering

– The study of how to develop correct, efficient, general-purpose programs in a reasonable amount of time.

Page 12: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● Software development cycle

Page 13: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development● Design

– Deciding what the program is going to look like.● Problem specification

– The task of stating precisely what a program is supposed to do.

– Breaking the program down into components● Implementation

– Writing code– Move from a description of a program to a

working program.

Page 14: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● Testing– Run the program– Verify that it does what it is supposed to do.

● Maintenance– Changes to make, new features to add, and bugs

to fix.– More iterations of the software development cycle

Page 15: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development● Top-down approach (for professionals)

– The entire program should be designed in exquisite (very) detail, then implemented, then tested.

● Making all decisions up front, we avoid wasting time implementing unnecessary components.

● Bottom-up approach (for beginners)– Design some simple component, implement it, test

it, expand the design very slightly.● Most software development falls between these two

extremes.● Encapsulation allows us to break up the software

development cycle.

Page 16: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

Page 17: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Software Development

● The ability to concentrate on a single component makes it much easier to rapidly develop correct, efficient, general-purpose code.

● Integrate the components in a high-level implementation phase and then test the entire system.

Page 18: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Encapsulation● Refer to : http://www.tutorialspoint.com/java/java_encapsulation.htm

● Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.

● Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

● Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.

● The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Page 19: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Example: Let us look at an example that depicts encapsulation:

/* File name : EncapTest.java */

public class EncapTest {

private String name;

private String idNum;

private int age;

public int getAge() {

return age;

}

public String getName() {

return name;

}

Page 20: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

public String getIdNum() { return idNum;

} public void setAge(int newAge) {

age = newAge; } public void setName(String newName) {

name = newName; } public void setIdNum(String newId) {

idNum = newId; }

}

Encapsulation Example (continued)

Page 21: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Encapsulation Example (continued)

● The public methods are the access points to this class fields from the outside java world.

● Normally these methods are referred as getters and setters (accessors and mutators).

● Therefore any class that wants to access the variables should access them through these getters and setters.

Page 22: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The variables of the EncapTest class can be access as below:

/* File name : RunEncap.java */

public class RunEncap {

public static void main(String args[]) {

EncapTest encap = new EncapTest(); // constructor

encap.setName("James");

encap.setAge(20);

encap.setIdNum("12343ms");

System.out.print("Name: " + encap.getName()

+ “; Age: "+ encap.getAge());

}

}

Page 23: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

This would produce following result:

Name: James; Age: 20

Page 24: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● Classes– Predominantly a description of a set of similar

objects– Encapsulated component of the program– We can create multiple instances of a class.– Each instance is a different object.

● In Java a class begins with an upper-case letter● A file must have the same name as the class

and a .java extension.

Page 25: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Page 26: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● Create two instances of the class of beetles● Create one instance of the class of dice

Page 27: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 28: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● An object has two kinds of components:– Fields

● Represent its current state● Field values vary from one instance to another.

– Methods● Actions it can perform● We ask objects to do things to themselves

– Example: we don't roll a die, we ask it to roll itself.

Page 29: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 30: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● Static– Not associated with an instance of a class.

● Instance field or instance variable– A different value for each instance of a class.

● Private– They cannot be accessed by methods in other

classes.– Example of information hiding

Page 31: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● By making such small, incremental changes to the code, we can avoid spending a lot of time hunting for bugs (program errors).

● Constructor– Method that initializes all of the fields of an object.– Same name as the class.

● In a nonstatic method, the current object can refer to itself with the keyword this.

Page 32: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 33: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 34: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 35: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● Default value– Java will initialize a field in an object.

● Numbers such as int and double the value is 0● Booleans the default is false.● Chars the default is unprintable character with

ASCII and 0 for Unicode.● Arrays the default is null.

Page 36: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● Other classes should be able to get at the fields of an object only through methods.– Accessor or getter

● Returns the value of some field– Mutator or setter

● Changes the value of some field within the object.

Page 37: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 38: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 39: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● Nonstatic fields or nonstatic methods are associated with instances.– We can use this only within nonstatic methods.– Nonstatic methods are sometimes called instance

methods.● A static method is associated with the entire

class rather than with individual instances.– Static methods are sometimes called class

methods.

Page 40: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

• To roll a die to generate 1, 2, 3, 4, 5 or 6 point.

Page 41: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

Page 42: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Classes and Objects

● When we are done with a class, we generate automatic documentation with javadoc

● Private fields are not shown because that information is not required outside the scope of the class.– Example of encapsulation.

● http://download.oracle.com/javase/7/docs/api/ has similar documentation for all of Java's hundreds of built-in classes.– Known as the application programming interface or API

Page 43: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 44: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-19, p19-20)

Page 45: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 46: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

● The toString() Method– When a nontrivial piece of code is needed

more than once, move it off into a separate encapsulated component.

● Example: String representations are common used to output object states.

– toString() returns a String representation of the current state of the Beetle.

Page 47: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

● If we have a variable bug referring to an instance of Beetle, we can print it with the statement:

System.out.println(bug.toString());

● Since toString() is so common, passing just the object to println() implies the use of the toString() method.

– Example: System.out.println(bug);● Possible Beetle representations

Page 48: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-20, p21-22)

Page 49: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 50: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 51: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-21, p23)

● The toString() method for the Die class:

Page 52: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-22, p23-25)

1 /** Beetle with parts for the Beetle game. */2 public class Beetle {34 /** True if this Beetle has a body. */5 private boolean body;67 /** Number of eyes this Beetle has, from 0-2. */8 private int eyes;910 /** Number of feelers this Beetle has, from 0-2. */11 private int feelers;

Page 53: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

12 13 /** True if this Beetle has a head. */14 private boolean head;1516 /** Number of legs this Beetle has, from 0-6. */17 private int legs;1819 /** True if this Beetle has a tail. */20 private boolean tail;21

Page 54: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 55: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 56: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 57: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 58: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 59: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-24, p26)

Page 60: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 61: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 62: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-26, p27)

Page 63: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-28, p28)

Page 64: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 65: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects (Fig. 1-29, p30)

Page 66: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Using Objects

Page 67: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Summary

● Three Principles– Encapsulation– Polymorphism– Inheritance

● Encapsulation– Division of a program into distinct components

(such as methods and classes) which have limited interaction.

Page 68: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Summary

● Software development cycle has three phases– Design– Implementation– Testing

● Top-down approach● Bottom-up approach● Encapsulation is enforced by information hiding.● Private fields of an object can be accessed only

within the object's class.

Page 69: Chapter 1 Encapsulation © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 1 Self-Study Homework

● Pages: 8-35● Exercises: 1.3, 1.5, 1.7, 1.8 ● Note: For each programming question, hand in

the error-free programs and screenshots of its execution results.