Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Object Oriented
Programming
Introduction to Object Oriented
Programming
1
An overview of OOP
2
The evolution of programming
techniques
People have been writing computer programs
since the 1940s
The oldest programming languages required
programmers to work with memory
addresses and to memorize awkward codes
associated with machine languages
3
Newer programming languages
more like natural languages
Easier for programmers to use
Allow programmers to name variable
Allow to create self-contained modules / program
segments that can be pieced together in a variety
of ways
Usually created by teams of programmers, each
developing his own reusable & connectable
program procedures
Writing several small modules is easier than
writing 1 large program
Large tasks are easier when you break into units
4
2 major techniques to develop
programs & procedures
1. Procedural programming
Focuses on the procedures that programmers create to
manipulate data
Focus on the action that are carried out. Eg getting input
data for an employee
Breaking down the process into manageable subtasks
2. Object oriented programming
Focuses on object and describes their features / attributes
and behaviors.
Attributes of an object – the features it has, the value of an
object’s attributes constitute the object’s state.
Behavior of an object – the thing it does
5
Object oriented approach
6
Two major types of OOP
1. Computer simulations
applications
2. Graphical User Interface (GUI)
Computer simulations
Example :
A city might want to develop a program that provides a
simulation of traffic patterns so as to better prevent
traffic tie-ups.
By creating a model with objects such as cars and
pedestrians that each contain their own data and rules
for behavior, the simulation can set into motion.
So, a computer can create a simulation of a real city at
rush hour
7
Graphical User Interface (GUI)
8
Understanding the programming
process
Object oriented approach involves 3
separate tasks:
1. Analyzing the system using an object-oriented
approach (object oriented analysis)
2. Designing the system using an object-oriented
approach (object oriented design)
3. Writing the programs using an object-oriented
approach (object oriented programming)
9
Writing a program involves:
10
With OOP :
You analyze the objects you’re working with and the tasks that
need to be performed with, and on, those objects
You pass messages to objects, requesting the objects to take
action
The same message works differently when applied to different
objects
A module / procedure can work appropriately with different types
of data it receives, without the need to write separate modules
Objects can share or inherit traits of objects that have already
been created, reducing the time it takes to create new objects
Encapsulation and information hiding are emphasized.
11
OOP terminology
Object Oriented Programming (OOP) emphasizes
the following concepts:
Class – describes a group or collection of objects with
common properties
Objects – an instance of a class
Inheritance – process of acquiring the traits of one’s
predecessors
Polymorphism – multiple methods with the same name but
act differently and appropriately when used with different
types of objects.
Encapsulation – process of combining all of an object’s
attributes and methods into a single package. It exposes
only the functional details, but hides the implementation
details of the class.
12
Object Oriented Programs
Implementation of all the collections of
objects that were modeled in the analysis and
design phase of the software development
process.
A real world problem consists of several
collections of entities interacting with one
another and with their surroundings
When solving a real-world problem, a
simplified representation of the problem is
used to study the problem and construct the
solution
The representation – model of the problem 13
A model is composed of objects, each one
representing a real-world entity
The model includes descriptions about these
objects and their interactions
A task of designing and constructing a model
is called modeling
14
Objects and Problem Solving
OO approach for problem
solving – all phases of the
A simplification of
process are based on
problem solving
objects
process
Real-world
objects
Model of problem
Program
Software
objects
15
Part of the development
process
Grouping similar real-world objects into
collections of objects, modeling of these
collections and software implementation of
the corresponding classes. When the
program executes, objects of these classes
are created and made to interact among
themselves
16
Collection of real-world
objects
Collection A of
objects Model of class A Class A
17
Modules
18
General structure of program
Attributes Attributes
Operations Operations
Class A Class B
Attributes Attributes
Operations
Operations
Class C Class D
Program_1
19
Difference between
Structured Programming
Language
from
Object Oriented Programming is aOOP
subset of structured
programming. After objects are created in a program, you use
those objects and their methods to operate the program.
In structured programming, you have a program with many
methods in which you can use.
One difference between structured programming and object-
oriented programming is that structured programming uses the
data that is given to them through parameters, while in object-
oriented programming, the methods act upon the object's data
(fields).
This makes programming much easier because the fields are all
there and you do not have to make sure that the correct field is
passed to the correct method. All you have to do is call which
field you want to work with.
20
What is an object?
Object is a thing (tangible/intangible)
A program written in object oriented style will
consist of interacting objects.
Objects are given the responsibility of carrying
out specific tasks of the solution
Objects are models of the real-world entities
identified in the real-world environment of the
problem
Objects with similar characteristics are grouped
into collections called classes
21
What is an object?
Example :
A program to maintain bank accounts may
22
Every object has:
State – represented by the set of properties or
attributes and their associated values
Behavior – represented by the operations, also
known as method, of the object
Identity – which is a property that can help identify
an object identity
:Person
name = “Siti”
state
age = “20”
play()
behavior
stop()
23
What is an object?
Jack Jill
Inside the
Two Customer objects with
Customer Customer
object icon the names Jack and Jill
we indicate
the type of
an object.
24
Classes
In the real-world problem, a class describes a
collection of real-world entities or objects with
similar characteristics
The abstract descriptions of the collections of
objects are called classes (or class model)
A class is a template to create objects.
An object is called an instance of a class.
An object is an instance of exactly one class.
An instance of a class belongs to the class.
Example : The two Customer objects Jack and
Jill are instances of the Customer Class.
25
Classes
26
Classes
A Customer class with two Customer objects
Customer
Customer Customer
27
Example of Classes and Objects
Class Objects
28
Java Programming Basics
An object-oriented program uses objects
To use an object in a program, we first declare and
create an object.
29
Object Declaration
Every object we use in a program must be declared.
An object declaration designates the name of an object and the
class to which the object belongs.
Object declaration syntax:
Example:
30
Object Creation
No objects are actually created by the declaration.
Example:
Account account;
31
Distinction between object
declaration and object creation
Account account;
account = new Account ();
Account account;
The identifier account is declared account
and space is allocated in memory
32
Example of object declaration
and object creation
Customer profWu, drCafe;
profWu
profWu = new Customer ();
drCafe = profWu;
drCafe
33
Defining classes
class <classname>
{
[declaration;]
[methods;]
}
34
Class Definition Program
Template
Import Statements
Class Comment
Describe the class in the
javadoc format
{
Declarations
Declare data members shared
by multiple methods here,
outside of method declarations
. . .
Methods
} 35
Defining classes
class hello
{
public static void main (String args[])
{
System.out.println(“Hello, World!”);
}
}
36
Method
To instruct a class or an object to perform a task, we send a message to
it.
We cannot send a message to any class or object. We can send a
message only to the classes and objects that understand the message.
Class or object need a matching method to process the message it
receives.
Method is a sequence of instructions a class or an object follows to
perform a task.
A method defined for a class is called a class method, a method defined
for an object is an instance method.
Class method – for a task that pertains to all instances
Instance method – for a task that pertains to an individual instance
37
Class, Object and Method
<Class Name>
Class Method
<Object Name>
<Class Name>
Object Method
38
Defining Methods
39
Defining Methods
where
modifier specifies the scope(e.g.,public)
return-type specifies what type of value the method will
return. It can be a simple data type or a class
type
method-name an identifier
Parameter-list contains the variables to be passed. The
parameters must always be enclosed in
parentheses even if the parameter list is empty.
40
class Square{
private double width;
private double height;
Square(){
width = 0;
height = 0;
Method name
}
Return type
41
Coding Example
42
Example 1 : Program
FunTime
1. /* The program will allow you to draw a picture by
2. dragging a mouse. */
3. import javabook.*;
4. class FunTime
5. {
6. public static void main (String args[ ])
7. {
8. SketchPad doodleBoard;
9. doodleBoard = new SketchPad(); Creates a new
SketchPad object
10. doodleBoard.show(); doodleBoard
43
Explanation – Example 1
44
Example 2 - GradeBook
1. // GradeBook.java
2. // Class declaration with one method
3.
4. public class GradeBook
5. {
6. //display a welcome message to the GradeBook
7. public void displayMessage()
8. {
9. System.out.println(“Welcome to the Grade Book!”);
10. } //end method displayMessage
11.
12. } //end class GradeBook
45
1. // Fig. 3.2 GradeBookTest.java
2. // Create a GradeBook object and call its displayMessage method
3.
4. public class GradeBookTest
5. {
6. // main method begins program execution
7. public static void main (String args[])
8. {
9. // create a GradeBook Object and assign it
10. GradeBook myGradeBook = new Grade Book ( )
Use class instance
11.
creation expression to
12. //call myGradeBook’s displayMessage method create object of class
13. myGradeBook.displayMessage ( ); GradeBook
14. } // end main
15. Call method
16. } // end class GradeBookTest displayMessage
using GradeBook
object
Welcome to the Grade Book!
46
Benefits of OOP
Reusability (reusable components)
Reliability
Robustness
Extensibility
Maintainability
Reducing large problems to smaller, more
manageable problems
Better Analysis and Design of Complex
Applications
47