You are on page 1of 53

Object Oriented

Programming through
JAVA

Dr. K. Kavitha
Learning Objectives
 State what is meant by “Object Oriented
Programming”.
 Discuss the Object Oriented Problem solving
paradigm as a way of viewing the world.
 State and explain the key terms of object
orientation, such as ,
 Classes & Instances
 Methods & Messages
 Encapsulation & Information hiding
 Inheritance
Computer Programming
 The history of computer
programming is a steady move
away from machine-oriented
views of programming towards
concepts and metaphors that
more closely reflect the way in
which we ourselves understand
the world
3
Programming progression…

 Programming has progressed through:


 machine code
 assembly language
 machine-independent programming
languages
 procedures & functions
 objects

4
Machine language – Mark I

5
Machine Language

0000 1001 1100 0110 1010 1111 0101 1000


1010 1111 0101 1000 0000 1001 1100 0110
1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111

6
Assembly Language – PDP-11

7
Assembly Language – Macro-11

GCD: TST B
BEQ SIMPLE
MOV A, R5
SXT R4
DIV B, R4
MOV B, A
MOV R5, B
CALL GCD
SIMPLE: RETURN

8
Assembly Language – Macro-11

GCD: TST B
BEQ SIMPLE
MOV A, R5
SXT R4
DIV B, R4
MOV B, A
MOV R5, B
CALL GCD
SIMPLE: RETURN

9
Machine-Independent Programming Languages
– Fortran ! This example program solves for roots of the quadratic equation,
! ax^2 +bx +c =0,for given values of a, b and c.
!
PROGRAM bisection
IMPLICIT NONE
INTEGER :: iteration
DOUBLE PRECISION :: CC, Er, xl, x0, x0_old, xr

! Set convergence criterion and guess for xl, xr.


CC = 1.d-4
xl = 8.d-1
xr = 11.d-1

! Bisection method.
Er =CC +1
iteration = 0
DO WHILE (Er > CC)
iteration = iteration + 1

! Compute x0 and the error.


x0_old = x0
x0 = (xl + xr) / 2.d0
Er = DABS((x0 - x0_old)/x0)*100.d0
WRITE (*,10) iteration, x0_old, x0, Er
10 FORMAT (1X,I4,3(2X,E10.4)) this is partial…

10
Procedures & Functions – Pascal
program ValueArg(output);
{Shows how to arrange for a procedure to have arguments.}

procedure PrintInitials(First, Last : char);


{Within this procedure, the names First and Last represent the
argument values. We’ll call write to print them.}
begin
write(‘My initials are: ’);
write(First);
writeln(Last)
end; {PrintInitials}

begin
PrintInitials (‘D’, ‘C’); {Any two characters can be arguments.}
PrintInitials (‘Q’, ‘T’); {Like strings, characters are quoted.}
PrintInitials (‘&’, ‘#’)
end. {ValueArg}

11
Objects
 (This example is from Java)
class Time {

private int hour, minute;

public Time (int h, int m) {


hour = h;
minute = m;
}

public void addMinutes (int m) {


int totalMinutes =
((60*hour) + minute + m) % (24*60);
if (totalMinutes<0)
totalMinutes = totalMinutes + (24*60);
hour = totalMinutes / 60;
minute = totalMinutes % 60;
}
}
this is partial…
12
“Intrinsic Power” vs. “Effective Power”
 This progression is not a matter of “intrinsic power”
 Anything you can do with a minimally capable
computer language, you can theoretically do with
any other minimally capable computer language
 But that is like saying a shovel is theoretically as
capable as a tractor. In practice, using a shovel
might make things very hard…

13
Why is Object Oriented
Programming a Paradigm?
The approach to
problem solving

Object Oriented Provides a view How to perform


Programming of computations in
Paradigm problem solving

How the data is to


be structured
internally in
computer for
problem solving
Object Oriented Programming

Object Oriented Programming Paradigm:


as a way of viewing the world.
 Suppose an individual named Chris wishes to
send flowers to a friend named Robin, who
lives in another city.
 Chris simply walks to a nearby flower shop
and initiates a sequence of events that
results in Robin receiving the flowers.
Agents and Communities

16
Agents and Communities

 The mechanism that was used to solve the flower


problem was to find an appropriate agent (florist) and to
pass to this agent a message containing a request.
 It is the responsibility of the florist to satisfy the request.
There is some method used by the florist to do this.
 Chris does not need to know the particular method that
the florist will use to satisfy the request; indeed, often the
person making a request does not want to know the
details.
 This information is usually hidden from inspection.

17
Agents and Communities
 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.
Agents and Communities
Agents and Communities
What is an object?

 Tangible Things as a car, printer, ...


 Roles as employee, boss, ...
 Incidents as flight, overflow, ...
 Interactions as contract, sale, ...
 Specifications as colour, shape, …

21
What is Object Oriented
Programming?
 Identifying objects and
assigning responsibilities to
these objects.
 Objects communicate to
An object is like a
other objects by sending
black box.
messages.
The internal
 Messages are received by
details are
the methods of an object
hidden.

22
So, what are objects?

 an object represents an individual,


identifiable item, unit, or entity, either real or
abstract, with a well-defined role in the
problem domain.
Or
 An "object" is anything to which a concept
applies.
Etc.

23
Why do we care about objects?

 Modularity - large software projects can


be split up in smaller pieces.
 Reuseability - Programs can be
assembled from pre-written software
components.
 Extensibility - New software components
can be written or developed from existing
ones.

24
Example: The Person class
#include<string>
#include<iostream>
class Person{
char name[20]; private
int yearOfBirth; data

public:
void displayDetails() {
system.out.print(name + " born in "
+ yearOfBirth );
} public
//... processes

};
The two parts of an object

Object = Data + Methods


or to say the same differently:

An object has the responsibility to know and the


responsibility to do.

= +
26
Basic Terminology

 Abstraction is the representation of the


essential features of an object. These are
‘encapsulated’ into an abstract data type.
 Encapsulation is the practice of including in
an object everything it needs hidden from
other objects. The internal state is usually not
accessible by other objects.

27
Basic Terminology:
Inheritance
 Inheritance means that one class inherits the
characteristics of another class.
This is also called a “is a” relationship:

A car is a vehicle

A dog is an animal

A teacher is a person

28
Basic Terminology:
Polymorphism
 Polymorphism means “having many forms”. It
allows different objects to respond to the
same message in different ways, the
response specific to the type of the object.

E.g. the message displayDetails() of the Person class should give


different results when send to a Student object (e.g. the enrolment
number).

29
Basic Terminology:
Aggregation
 Aggregation describes a “has a” relationship.
One object is a part of another object.
A car has wheels.

 We distinguish between composite


aggregation (the composite “owns” the part)
and shared aggregation (the part is shared by
more then one composite).

30
Basic Terminology:
Behaviour and Messages
 The most important aspect of an object is its
behaviour (the things it can do). A behaviour
is initiated by sending a message to the
object (usually by calling a method).

31
Messages and Methods
 The chain reaction that ultimately resulted in
the solution to Chris's problem began with a
request given to the florist.
 This request lead to other requests, which
lead to still more requests, until the flowers
ultimately reached Chris's friend Robin.
 We see, therefore, that members of this
community interact with each other by making
requests. So, our next principle of object-
oriented problem solving is the vehicle used
to indicate an action to be performed.
Messages and Methods
 Action is initiated in object-oriented programming 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.
Messages and Methods

 Note the important principle of information


hiding in regard to message passing that is, the
client sending the request need not know the
actual means by which the request will be
honoured.
Messages and Methods
 There is another principle that we see is implicit in
message passing. If there is a task to perform, the
first thought of the client is to find somebody else he
or she can ask to do the work.
 This may be difficult for many programmers with
extensive experience in conventional techniques.
Frequently, a difficult hurdle to overcome is the idea
in the programmer's mind that he or she must write
everything and not use the services of others.
 An important part of object-oriented programming is
the development of reusable components, and an
important first step in the use of reusable
components is a willingness to trust software written
by others.
Messages and Methods
 Messages versus Procedure Calls
 Information hiding is also an important aspect of
programming in conventional languages. In what
sense is a message dierent from, say, a
procedure call? In both cases, there is a set of
well-dened steps that will be initiated following
the request. But, there are two important
distinctions.
Messages and Methods

 Messages versus Procedure Calls(1)


 The first is that in a message there is a
designated receiver for that message; the
receiver is some object to which the message is
sent. In a procedure call, there is no designated
receiver.
Messages and Methods
 Messages versus Procedure Calls(2)
 The second is that the interpretation of the message
(that is, the method used to respond to the message)
is determined by the receiver and can vary with
different receivers. Chris could give a message to a
friend named Elizabeth, for example, and she will
understand it and a satisfactory outcome will be
produced. However, the method Elizabeth uses to
satisfy the request will be different from that used by
florist in response to the same request.
 If Chris were to ask a dentist, to send flowers to
Robin. If he understands the request at all, he will
probably issue an appropriate error diagnostic.
Messages and Methods
 Messages versus Procedure Calls
 The distinction between message passing and
procedure calling is that, in message passing,
there is a designated receiver, and the
interpretation, the selection of a method to
execute in response to the message, may vary
with different receivers.
Messages and Methods
 Messages versus Procedure Calls
 Usually, the specific receiver for any given
message will not be known until run time, so the
determination of which method to invoke cannot
be made until then. Thus, we say there is late
binding between the message (function or
procedure name) and the code fragment
(method) used to respond to the message. This
situation is in contrast to the very early (compile-
time or link-time) binding of name to code
fragment in conventional procedure calls.
Responsibilities
 A fundamental concept in object-oriented
programming is to describe behavior in terms of
responsibilities. Chris's request for action
indicates only the desired out- come (flowers sent
to Robin). The florist is free to pursue any
technique that achieves the desired objective,
and in doing so will not be hampered by
interference from Chris.
Responsibilities
 By discussing a problem in terms of responsibilities
we increase the level of abstraction. This permits
greater independence between objects, a critical
factor in solving complex problems. The entire
collection of responsibilities associated with an
object is often described by the term protocol.
 Abstraction is the purposeful suppression, or
hiding, of some details of a process or artifact, in
order to bring out more clearly other aspects,
details, or structure.
Responsibilities
 An object oriented program requests data
structures (that is, objects) to perform a service.
This difference between viewing software in
traditional, structured terms and viewing it from
an object-oriented perspective can be
summarized by a twist on a well-known quote:
 Ask not what you can do to your data
structures,but rather ask what your data
structures can do for you.
Class Hierarchies Inheritance
 Chris has more information about the florist
(Fred) not necessarily because Fred is a florist
but because he is a shopkeeper. Chris knows, for
example, that a transfer of money will be part of
the transaction, and that in return for payment
Fred will offer a receipt. These actions are true of
grocers, stationers, and other shopkeepers.
Since the category Florist is a more specialized
form of the category Shopkeeper, any knowledge
Chris has of Shopkeepers is also true of Florists
and hence of Fred.
Class Hierarchies Inheritance

 A Human is a Mammal (therefore they


nurse their young and have hair), and a
Mammal is an Animal (therefore it
breathes oxygen), and an Animal is a
Material Object (therefore it has mass and
weight). Thus, quite a lot of knowledge
that Chris has that is applicable to Fred is
not directly associated with him, or even
with the category Florist.
46
Class Hierarchies Inheritance
The two steps of Object Oriented
Programming
 Making Classes: Creating, extending or
reusing abstract data types.

 Making Objects interact: Creating objects


from abstract data types and defining their
relationships.

51
Object Oriented approach as a method to
promote reusability of software
Features of JAVA

 Java is Object-Oriented from the Ground Up


 Java has the elegance that comes from being
designed after other OO languages had been in use
for many years
 Java has strong type checking
 Java handles its own memory allocation
 Java’s syntax is “standard” (similar to C and C++)
 Java is a good teaching language, but it (or
something close) will also be seen by students in
industry
54
Object-Oriented Programming in Industry

 Large projects are routinely programmed


using object-oriented languages
nowadays
 MS-Windows and applications in MS-
Office – all developed using object-
oriented languages
 This is the world into which our students
are graduating…

55
Conclusions
 Object-oriented programming provides a superior
way of organizing programming projects
 It encourages a high degree of modularity in
programming, making large projects easier to
implement
 It provides powerful techniques like inheritance and
polymorphism to help organize and reuse code
 Object-oriented languages like Java both provide
a good environment for beginning students to
learn programming, and match real-world
developments in computer programming

56

You might also like