No Slide Title

Download Report

Transcript No Slide Title

Information Technology and
Computing M301-Part 1
Block 1:
Introduction to Java
Tariq M. Khan
Yahoo Group: www.groups.yahoo.com/group/tariqkhan_m301
http://tech.groups.yahoo.com/group/tariqkhan_m301
Mobile: 0509372191
Arab Open University
1
Object-Oriented Paradigm


Paradigm: “example” or “model”.
 Paradigm sentence would help you
remember how to conjugate a verb in a
foreign language
A model is an example that helps you
understand how the world works
2
Installing Java IDE (SunOne studio)

If you do not already have Java Integrated Environment
(IDE) on your computer; following discussion will interest
you
There are two separate pieces of software needed to be
installed
 1. Java Runtime Environment
 2. SunOne Studio 4CE
Available for download from
http://www.4shared.com/dir/928236/27909998/sharing.html

3
Basics of a Typical Java
Environment
Java programs normally undergo five phases
Edit
Programmer writes program (and stores program on disk)
Compile
Compiler creates bytecodes from program
Load
Class loader stores bytecodes in memory
Verify
Verifier ensures bytecodes do not violate security
requirements
Execute
Interpreter translates bytecodes into machine language
4
Phase 1
Editor
Disk
Program is created in
an editor and stored
on disk in a file ending
with .java.
Phase 2
Compiler
Disk
Compiler creates
bytecodes and stores
them on disk in a file
ending with .class.
Primary
Memory
Phase 3
Class Loader
Disk
Phase 4
Bytecode
Verifier
. ..
..
.
Class loader reads
.class files
containing
bytecodes from
disk and puts
those bytecodes
in memory.
Primary
Memory
Bytecode verifier
confirms that all
bytecodes are valid
and do not violate
Java’s security
restrictions.
. ..
..
.
Primary
Memory
Phase 5
Interpreter
. ..
..
.
Interpreter reads
bytecodes and
translates them into
a language that the
computer can
understand,
possibly storing
data values as the
program executes.
Typical Java environment.
5
Thinking About Objects:
Introduction to Object Technology
and the Unified Modeling Language


Object orientation
Unified Modeling Language (UML)
 Graphical language that uses common
notation
 Allows developers to represent objectoriented designs
6
Thinking About Objects (cont.)

Objects
 Reusable software components that
model real-world items
 Look all around you

People, animals, plants, cars, etc.
 Attributes
 Size, shape, color, weight, etc.
 Behaviors
 Babies cry, crawl, sleep, etc.
7
Thinking About Objects (cont.)


Object-oriented design (OOD)
 Models real-world objects
 Models communication among objects
Object-oriented language
 Programming in object oriented
languages is called object-oriented
programming (OOP)
 Java, C++ and SmallTalk etc.
8
Thinking About Objects (cont.)

Object-Oriented Analysis and Design
(OOA/D)
 Essential for large programs
 Analyze program requirements, then
develop solution
 UML

Unified Modeling Language
9
Illustration of OOP Concepts :
Sending Flowers to a Friend

Suppose I wish to send flowers to a friend,
Sally, who lives in a city many miles away.
 What
should I do?
10
Agents and Communities

Solution: Find an appropriate agent, namely
Flora, and pass to her a message containing
my request
 It is the responsibility of Flora to
satisfy my request
 There is some method – some algorithm
or some set of operations – used to
satisfy my request

This information, i.e., details, is usually hidden from
my inspection.
11
The community of agents helping me
Gardener
Tom
Flower Arranger
Me
Grower
Delivery Person
Flora
Wholesaler
Sally’s Florist
12
An Observation

An object-oriented program is structured
as a community of interacting agents,
called objects.
 Each object has a role to play.
 Each object provides a service, or
performs an action, that is used by
other members of the community.
13
Messages and Methods


Action is initiated in OOP by the transmission of a
message to an agent (an object) responsible for
the action.
 The message encodes the request for an action
and is accompanied by any additional
information (arguments) needed to carry out
the request.
The receiver is the object to whom the message is
sent. If the receiver accepts the message, it
accepts the responsibility to carry out the
indicated action.
 In response to a message, the receiver will
perform some method to satisfy the request.
14
Information Hiding Principle


The client sending the request need not
know the actual means by which the
request will be honored.
An important part of the OOP is the
development of reusable components
 “thrusting” others
15
Responsibilities

Describe behavior in terms of
responsibilities
 increases level of abstraction
 greater independence between objects,
a critical factor in solving complex
problems
 Protocol: The entire collection of
responsibilities associated with an
object
16
Classes and Instances

We can use the term Florist to represent
the category (or class) of all florists.
 I am able to make certain assumptions
because I have information about
florists in general, and I expect that
Flora, being an instance of this category,
will fit the general pattern.
17
Objects and Classes



All objects are instances of a class
The method invoked by an object in
response to a message is determined by
the class of the receiver
All objects of a given class use the same
method in response to similar messages.
18
Class Hierarchies - Inheritance


I have more information about Flora – not
necessarily because she is a florist but
because she is a shopkeeper.
One way to think about how I have
organized my knowledge of Flora is in
terms of a hierarchy of categories:
19
The categories surrounding Flora
Material Object
Animal
Mammal
Human
Shopkeeper
Florist
Flora
20
A class hierarchy of various
material objects
Material Object
Dog
Animal
Plant
Mammal
Flower
Human
Platypus
Shopkeeper Artist
Florist
Flash
Flora
Carnation
Dentist
Painter
Liz
Adam
Phyl
Sally’s Flowers
More abstract classes are listed near the top of the tree
More specific classes and individuals are listed near the bottom.
21
Inheritance and subclasses


The principle that knowledge of a more general
category is also applicable to a more specific
category is called inheritance.
 The class Florist will inherit attributes of the
class (or category) Shopkeeper.
Classes can be organized into a hierarchical
inheritance structure.
 A child class (or subclass) will inherit attributes
from a parent class higher in the hierarchy. An
abstract parent class is a class for which there
are no direct instances; it is used only to create
subclasses.
22
Method Binding, Overriding, and
Exceptions


Exceptions to a general rule: Consider Phyl
 Phyl gives birth to live offspring yet Phyl lays
eggs.
The information contained in a subclass can
override information inherited from a parent class
 Most often, implementations of this approach
take the form of a method in the subclass
having the same name as the method in the
parent class
23
Searching for the appropriate
response


If no appropriate method is found, the search
conducted in the parent class of this class.
The search continues up the parent class chain
until either a method is found or the parent class
chain is exhausted.
 In the former case, the method is executed; in
the latter case, an error message is issued.
 If methods with the same name can be found
higher in the class hierarchy, the method
executed is said to override the inherited
behavior.
24
Polymorphism and Information Hiding


That my wife, Liz and my florist Flora will
respond to my message by different
methods is an example of one form of
Polymorphism.
That I do not, need not, know exactly what
method Flora will use to honor my message
is an example of information hiding.
25
Description of Object-Oriented
Programming

Object-oriented programming is based on the
principle of recursive design.
1.Everything is an object
2.Objects perform computation by making
requests of each other through the passing of
messages
3.Every object has it's own memory, which
consists of other objects.
4.Every object is an instance of a class. A class
groups similar objects.
5.The class is the repository for behavior
associated with an object
6.Classes are organized into singly-rooted tree
structure, called an inheritance hierarchy.
26
Pointers and Memory Addresses


In contrast, in OO framework we never mention
memory addresses, variables, assignments, or any
of the conventional programming terms.
 We speak of objects, messages, responsibility
for some action.
Instead of a bit-grinding processor . . . plundering
data structures, we have a universe of wellbehaved objects that courteously ask each other
to carry out their various desires.
27
The Power of OOP
Easier to teach OOP concepts to computer
novices than to computer professionals.
 Programming is as like the task of
“training” the universe of agents to
interact smoothly with each other, each
providing a certain small and well-defined
service to the others, each contributing to
the effective execution of the whole.
28