Download Chapter 1 Slides

Document related concepts
no text concepts found
Transcript
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
Software Development
●
Essential that programs be correct.
–
●
Efficient
–
●
No more time or memory than necessary
General-purpose
–
●
Contains no bugs
Ability to build upon program for future use.
Rapidly developed
Software Development
●
Tradeoffs in a computer program
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.
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
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
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.
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.
Software Development
●
Encapsulation
–
●
Polymorphism (poly: multi; morphism: forms)
–
●
The division of a program into distinct
components which have limited interaction.
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.
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.
Software Development
●
Software development cycle
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.
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
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.
Software Development
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.
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.
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;
}
Encapsulation Example (continued)
}
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)
●
●
●
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.
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());
}
}
This would produce following result:
Name: James; Age: 20
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.
Classes and Objects
●
Create two instances of the class of beetles
●
Create one instance of the class of dice
Classes and Objects
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.
Classes and Objects
Classes and Objects
●
Static
–
●
Instance field or instance variable
–
●
Not associated with an instance of a class.
A different value for each instance of a class.
Private
–
They cannot be accessed by methods in other
classes.
–
Example of information hiding
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.
Classes and Objects
Classes and Objects
Classes and Objects
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.
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.
Classes and Objects
Classes and Objects
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.
Classes and Objects
•
To roll a die to generate 1, 2, 3, 4, 5 or 6 point.
Classes and Objects
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
Using Objects
Using Objects (Fig. 1-19, p19-20)
Using Objects
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.
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
Using Objects (Fig. 1-20, p21-22)
Using Objects
Using Objects
Using Objects (Fig. 1-21, p23)
●
The toString() method for the Die class:
Using Objects (Fig. 1-22, p23-25)
1 /** Beetle with parts for the Beetle game. */
2 public class Beetle {
3
4 /** True if this Beetle has a body. */
5 private boolean body;
6
7 /** Number of eyes this Beetle has, from 0-2. */
8 private int eyes;
9
10 /** Number of feelers this Beetle has, from 0-2. */
11 private int feelers;
Using Objects
12
13
14
15
16
17
18
19
20
21
/** True if this Beetle has a head. */
private boolean head;
/** Number of legs this Beetle has, from 0-6. */
private int legs;
/** True if this Beetle has a tail. */
private boolean tail;
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects (Fig. 1-24, p26)
Using Objects
Using Objects
Using Objects (Fig. 1-26, p27)
Using Objects (Fig. 1-28, p28)
Using Objects
Using Objects (Fig. 1-29, p30)
Using Objects
Summary
●
●
Three Principles
–
Encapsulation
–
Polymorphism
–
Inheritance
Encapsulation
–
Division of a program into distinct components
(such as methods and classes) which have
limited interaction.
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.
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.