Sei sulla pagina 1di 47

MTS 2023

Object Oriented
Programming
Introduction to Object Oriented
Programming

1
An overview of OOP

 OOP is a style programming that focuses on


an application’s data and the methods you
need to manipulate that data

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

 Defining the object’s needed to accomplish a


task and developing the objects so that each
object maintains its own data and carries out
tasks when another object request them.

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)

 It is easy to think of the components a user


manipulates on a computer screen such as
buttons and scroll bars, as similar to real
world objects
 Each GUI contains data – eg button with a
specific size and color
 Each object contains behavior – eg button
can be clicked

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:

1. Identify all the objects you want to manipulate and


how they relate to each other – data modeling
2. Create a general category for the object – class
and how they will communicate with each other
3. Code the statements in programming languages
4. Test the program
5. Put the program into production

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

Algorithm & Model of


data design 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

Model of class B Class B


Collection B of
objects

Real world Model of problem Software


implementation

17
Modules

 A problem is often too complex to deal with a


single unit
 A general approach is to divide the problem
into smaller problems that are easier to solve
 The partitioning of a problem into smaller
parts is known as decomposition
 These small parts are called modules, which
are easier to manage.

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

have many Account, Customer, Transaction


and ATM objects.
 An Account object may consist of data such as

account number, owner, date opened, initial


balance etc.
 Program also can has many objects of the

same type. Two Customer objects with the


names of Jack and Jill

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?

<Object Name> account 1

This is an Account object


named account1

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

 A class must be defined before you can create


an instance (object) of the class.
 One class is defined, we can create as many
objects of the class as a program requires.
 Every class defines:
 Attributes (data declaration)
 One or more operations (functions or method)

26
Classes
A Customer class with two Customer objects

Customer

This is the Customer class

This line shows an instance of


relationship; e.g Jill is an
Jack Jill instance of Customer

Customer Customer

27
Example of Classes and Objects

Class Objects

Satria Neo, MyVi, Honda Civic,


Car
Toyota Vios

Orchid, Rose, Bougainvillea,


Flower
Morning Glory

Circle, Square, Rectangle,


Shape
Triangle

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:

<class name> <object names>;

 Example:

Customer Jack, Jill;

30
Object Creation
 No objects are actually created by the declaration.
 Example:
Account account;

account is used to refer to an Account object, but the actual Account


object is not yet created.
 We create an object by invoking new operation.
 The syntax for new is

<object name> = new <class name> (<arguments>);

<object name> The name of a declared object


<class name> The name of the class to which the object belongs
<arguments> A sequences of values passed to the new operation

31
Distinction between object
declaration and object creation
Account account;
account = new Account ();

Process State of memory

Account account;
The identifier account is declared account
and space is allocated in memory

account = new Account ();


account
An Account object is created and
the identifier account is set to refer
to it. Account

32
Example of object declaration
and object creation
Customer profWu, drCafe;
profWu
profWu = new Customer ();
drCafe = profWu;
drCafe

Customer profWu, drCafe;


profWu = new Customer (); profWu
drCafe = profWu;
drCafe Customer

Customer profWu, drCafe;


profWu = new Customer (); profWu
drCafe = profWu;
drCafe Customer

33
Defining classes

 A basic class definition takes the form:

class <classname>
{
[declaration;]
[methods;]
}

34
Class Definition Program
Template
Import Statements

Class Comment
Describe the class in the
javadoc format

class Class Name

{
Declarations
Declare data members shared
by multiple methods here,
outside of method declarations

. . .
Methods

} 35
Defining classes

 Example of a Java program (a class)

class hello
{
public static void main (String args[])
{
System.out.println(“Hello, World!”);
}
}

 The above code defines one class(hello) and one


method (main()).

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

The dotted line signifies


that you cannot send
instance messages to a
class

<Object Name>

<Class Name>
Object Method

38
Defining Methods

 A method in Java takes the basic form:

[modifier] return-type method-name (parameter list)


{
[statements;]
}

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.

 Instance methods are declared WITHOUT the static modifier


 Class methods are declared WITH the static modifier

40
class Square{
private double width;
private double height;

Square(){
width = 0;
height = 0;
Method name
}
Return type

Access control public double wide(){


return width * height;
}
}

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

11. } This statement


12. } makes doodleBoard
appear on the screen

43
Explanation – Example 1

 The program consists of 2 classes:


 FunTime : defined in the program
 SketchPad : predefined outside the program (in a
place called the javabook package).
 This program opens a SketchPad window
named doodleBoard and makes it appear on
the screen by sending the message show to
it

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

Potrebbero piacerti anche