You are on page 1of 30

DR.

JOE HENRY OBIT

Labuan School of Informatics Science

Universiti Malaysia Sabah Labuan International Campus


2

Java is Object Oriented (OO): dividing programs into modules (objects) helps manage software complexity. Java is robust: errors in Java don't usually cause system crashes as in other languages. Java is platform independent: programs run without changes on all different platforms. Java is a distributed language: programs can easily be run on computer networks. Java is a secure language: it guards against viruses and other untrusted code.
Universiti Malaysia Sabah 3
Labuan International Campus Labuan School of Informatics Science

An Object-oriented program (OOP) is a set of interacting objects that models a collection of real world objects.
Me MyVi IT00203 Students

DELL Laptop

Whereas this model and a computer program is dynamic: its objects communicate with each other and change over time.
Universiti Malaysia Sabah 4
Labuan International Campus Labuan School of Informatics Science

Now, its your turn to think of some scenarios with a few objects..

Universiti Malaysia Sabah


Labuan International Campus Labuan School of Informatics Science 5

An object is any thing whatsoever. It can be physical (Car, Money); mental (Idea); natural (Animal, Flower); artificial (ATM).

Universiti Malaysia Sabah 6


Labuan International Campus Labuan School of Informatics Science

We use UML (Unified Modeling Language) to depict objects.

id: Type : ATM

An objects label consists of its id and its type In UML objects are represented by rectangles Labeled with a two part label of the form id:Type.

christy : Account
Universiti Malaysia Sabah 7
Labuan International Campus Labuan School of Informatics Science

Objects have certain characteristic attributes which have certain values. An objects collection of attributes and values is sometimes called its state. Student

Name: Christian John Course: Multimedia PNGK: 3.00

Name: Emily Victor Course: E-Commerce PNGK: 3.57


Universiti Malaysia Sabah

Labuan International Campus Labuan School of Informatics Science

In UML, an objects attributes are represented in the second partition of the rectangle.

Two ATM objects showing their attributes and values.


Universiti Malaysia Sabah 9
Labuan International Campus Labuan School of Informatics Science

student1:Student
name= Christian John course = Multimedia CGPA = 3.00

student2:Student
name= Emily Victor course = E-Commerce

CGPA = 3.57

Universiti Malaysia Sabah 10


Labuan International Campus Labuan School of Informatics Science

Objects have characteristic actions or behaviors. ACTIONS associated with an object can be used to send messages--changeCourse(ECommerce)--to the object. Arguments (E-Commerce) is data value that can be passed as part of the message.
changeCourse(E-Commerce)
A message is represented as an arrow.
Universiti Malaysia Sabah 11
Labuan International Campus Labuan School of Informatics Science

student1:Student
name= Christian John course = Multimedia CGPA = 3.00

A customer is trying to check his account balance through an ATM machine in the hotel lobby.

This UML diagram illustrates a simple ATM Transaction.


Universiti Malaysia Sabah 12
Labuan International Campus Labuan School of Informatics Science

A class (Rectangle) is a blueprint or template of all objects of a certain type. It encapsulates the attributes and actions that characterize a certain type of object. Like a cookie cutter for a cookie, it gives a general type of rectangle. An object is an instance of a class.

Universiti Malaysia Sabah 13

A UML Class Diagram

Labuan International Campus Labuan School of Informatics Science

A variable == an attribute, is a named memory location that can store a certain type of value. A method == an action, is a named chunk of code that can be invoked to perform a certain predefined set of actions.

Variables Methods
Universiti Malaysia Sabah 14
Labuan International Campus Labuan School of Informatics Science

A instance variable (or instance method) is a variable or method that belongs to an object. A class variable (or class method) is a variable or method that is associated with the class itself.
Class variable

Instance variables

rect2

rect1

The Rectangle class and two of its instances. Note that class variables are underlined in UML.
Universiti Malaysia Sabah
Labuan International Campus Labuan School of Informatics Science

15

A constructor is a special method, associated with the class, that is used to create instances of that class. Example: We call the Rectangle() constructor to create an instance with length=25, width=10 and located at x=100 and y=50.

Constructing a Rectangle instance.


Universiti Malaysia Sabah 16
Labuan International Campus Labuan School of Informatics Science

How are classes related

to each other?

Universiti Malaysia Sabah


Labuan International Campus Labuan School of Informatics Science 17

Classes can be arranged in a hierarchy from

most general to most specific. A subclass is more specific than its superclass.
Classes Superclass

Subclasses

A hierarchy of Java classes.


Universiti Malaysia Sabah 18
Labuan International Campus Labuan School of Informatics Science

A subclass inherits elements (variables and methods) from its superclasses. Inherited elements can be specialized in the subclass.

Universiti Malaysia Sabah

The ChessPiece hierarchy.


19

Labuan International Campus Labuan School of Informatics Science

What is an object-oriented program composed of? Many objects that communicating with each other. What are the important principles in designing an OO program?

Universiti Malaysia Sabah


Labuan International Campus Labuan School of Informatics Science 20

Divide-and-Conquer Principle
Problem Solving: Break problems (programs) into

small, manageable tasks. Example: Sales agent, shipping clerk.

Universiti Malaysia Sabah 21


Labuan International Campus Labuan School of Informatics Science

Encapsulation Principle
Each object knows how to solve its task and has

the information it needs. Example: A dentist encapsulates the expertise needed to diagnose and treat a toothache. Sales agent is the sales expert. The shipping clerk is the shipping expert.

Universiti Malaysia Sabah 22


Labuan International Campus Labuan School of Informatics Science

Interface Principle
Each object has a well-defined interface, so you

know how to interact with it. Example: Digital vs. analog clock interfaces.

Example: The sales agent must be told the name

and part number of the software.


Universiti Malaysia Sabah 23
Labuan International Campus Labuan School of Informatics Science

Information Hiding Principle


In order to enable objects to work together

cooperatively, objects hide details of their expertise. Example: Customer neednt know how the sales agent records the order. To use the analog watch, we neednt know how its time-keeping mechanism works.

Universiti Malaysia Sabah 24


Labuan International Campus Labuan School of Informatics Science

Generality Principle
Objects are designed to solve a kind of task rather

than a singular task. Java comes with an extensive library of classes that specialize in performing certain kinds of input and output operations. Example: Sales agents sell all kinds of stuff.

Universiti Malaysia Sabah 25


Labuan International Campus Labuan School of Informatics Science

Extensibility Principle
An objects expertise can be extended or

specialized. Example: One sales agent specializes in software sales and another in hardware sales.

Universiti Malaysia Sabah 26


Labuan International Campus Labuan School of Informatics Science

Universiti Malaysia Sabah


Labuan International Campus Labuan School of Informatics Science 27

A(n) ________ in a Java program encapsulates programs ________ and __________. A ________ is a template of an object. A _______ is a special method used to construct objects. Objects interact with each other by sending __________ back and forth.
Universiti Malaysia Sabah
Labuan International Campus Labuan School of Informatics Science 28

A(n) __________ is a location in memory where a value is stored. Class inheritance means that subclass inherits characteristics and behaviors from their __________.

Universiti Malaysia Sabah


Labuan International Campus Labuan School of Informatics Science 29

__________ diagrams are used to illustrate object-oriented design. Dividing a problem or a task into parts is an example of the ___________ principle. Designing a class so that it shields certain parts of an object from other objects is an example of the _________ principle.
Universiti Malaysia Sabah
Labuan International Campus Labuan School of Informatics Science 30

You might also like