Object-Oriented Programming Concepts

parentpitaSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

81 views

Object-Oriented Programming
Concepts
Four important concepts make up the
Object-Oriented Programming para-
digm:
1.Object-oriented programs are made
up fromcollections of Objects.
2.These objects communicate with one
another in order to performthe pro-
grams tasks.This communication is
by way of Messages.
3.Objects are defined by templates
(type-specifiers) called Classes.
4.Classes can Inherit part of their
behaviour fromancestor classes
(SuperClasses).
Objects
The real world is made up of objects:
students,aeroplanes,rocks,lecturers,
etc.
These all have  state and they all have
 behaviour.
A Software Object is a transference of
this idea to programming:
A Method is simply a function or proce-
dure that belongs to a software object.
(The term method is very common in OOP.)
A Software Object (or simply Object) is
a software bundle of variables (the state)
and related methods (the behaviour).
The user of the object can only access it
via its public Application Programming
Interface (API).The public methods
(and sometimes variables) of the API
are allowed to change the internal state
of the object.
The user cannot change the internal
state of the object directly.
Data Hiding/Encapsulation.
Public
API
Private
Implementation
Details
private
methods
and variables
A Counter object.Models a digital up/down
counter.
 2 hidden pieces of state:
 currentCount, countDir.
 3 publicly accessible methods making
up the API: setCountDirection,
 clearCounter, applyClock.
currentCount
countDir
s
e
t
C
o
u
n
t
D
i
r
e
c
t
i
o
n
a
p
p
l
y
C
l
o
c
k
clearCounter
Once the API is defined,you have freedomto
change the internal implementation at any time.
The API is the  contract between the object
designer and the object user.
A  BankBalance object.Models a bank bal-
ance.
 1 hidden piece of state:
 currentBalance,the amount in the
account.
 1 hidden method: calcInterest.
 2 publicly accessible methods making
up the API: depositCash,
 withdrawCash.
The user can only modify the balance via the
two publicly accessible methods.These will
have checks to prevent withdrawals froman
empty account,etc.
currentBalance
depositCash
withdrawCash
calcInterest
The Benefits of Objects
Encapsulating related variables and
methods into a neat software bundle is a
simple yet powerful idea with two major
benefits:
1.Modularity:The source code for an
object can be written and maintained
independently fromthe source code
for other objects.Also,an object can
be easily passed around the system.
2.Information Hiding:An object has
a public interface (API) that other
objects can use to communicate with
it.However,the object can maintain
private information and methods that
can be changed at any time without
affecting the other objects that
depend on it.
Messages
Software objects interact and communi-
cate by sending messages to each other.
If object A wants object B to perform
one of its methods,it sends a message to
B requesting that behaviour.
Sometimes the receiving object needs
more information so that it knows
exactly what to do.
Object B
Object A
Message
This information is passed along with
the message as a parameter to a method.
A message is made up fromthree parts:
1.The name of the object to which the
message is addressed,
(My  BankBalance Object),
2.The name of the method to perform,
(depositCash),
3.Any parameters needed,
(e.g.,400).
My  BankBalance Object
Object A
Message:depositCash(400)
Classes
A programusually needs many objects
of the same sort.A banking program
may have many
BankBalance
objects
which represent different customers,but
which all share the same pattern of
methods and variables.
We say that each unique
BankBalance
object is an Instance of the Class of
BankBalance
objects.
Every such object is independent of
every other,but they all share the same
layout.
We capture the commonality by defin-
ing a blueprint or template for all
objects of the same sort.This blueprint
is a Class Definition.
Note that a class is not itself an object
(usually!).In order to make a new user-
defined object you must performtwo
steps:
1.Write a class definition for the
object,setting out its variables and
methods,
2.Instantiate the class to create an
object (which is an instance of the
class).You may instantiate a class as
often as you like to create new
objects of the same type.
A Class is a blueprint,(template,proto-
type),that defines the methods and vari-
ables common to all objects of a certain
kind
Every new instance of the class will get
its own set of instance variables.
All objects instantiated fromthe same
class will share the instance methods of
the class.
Classes may also define Class Variables
and Class Methods.
1.A Class Variable is one which is
shared amongst all instances of the
class.
A class variable may be used,for example,
to represent the standard output streamof
an application.Since a programhas only a
single standard output stream,this is a good
use for such a variable.
In the Hello World application 
out
 is a
class variable representing the standard
output stream.It belongs to class 
System

and has method 
println
 (inter alia).
2.A Class Method is one which may be
called even if no instance of its class
has been created.
The 
main
 method in the Hello World
application is an example of a class
method.No instance of the class

HelloWorld
 is created by this application.
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
HelloWorld.java
class method 
main
 of
class variable 
out
 of
class 
HelloWorld

class 
System

Class Diagrams
1.Doughnut Diagrams,
2.Object Modeling Technique (OMT)
diagrams,
3.Unified Modeling Language (UML)
diagrams.
current-
depositCash
withdrawCash
calcInterest
Balance
Note:no
shading,hence
class diagram
BankBalance
public withdrawCash(amount)
public depositCash(amount)
private calcInterest()
private currentBalance
BankBalance
+withdrawCash(amount:integer)
+depositCash(amount:integer)
 calcInterest()
 currentBalance:integer
Inheritance
In Object-Oriented Programming
(OOP),classes may be defined in terms
of other classes.They may Inherit the
attributes of other classes and specialise
themas needed.

Matrix
 Class

RotationMatrix

Class

TranslationMatrix

Class

ScalingMatrix

Class
Superclass
Subclasses
Class
H
ierarchy
D
iagram
Each subclass inherits state (in the form
of variable declarations) fromits super-
class.Hence
RotationMatrices
,
Transla-
tionMatrices
and
ScalingMatrices
all get
a copy of the variables of class
Matrix
.
Each subclass also inherits all the meth-
ods of its superclass.Hence,any meth-
ods defined for
Matrix
are automatically
available to all its subclasses.
Subclasses get the variables and meth-
ods of their superclass for free.How-
ever,they can specialise their behaviour
by adding new methods and/or varia-
bles.
They can also override inherited meth-
ods and provide specialised behaviour
for these methods.The subclass imple-
mentation replaces the superclass imple-
mentation with the same name.
The Benefits of Inheritance
1.Subclasses provide specialised
behaviours fromthe basis of com-
mon elements provided by the super-
class.
Using inheritance,programmers
can reuse the code in the superclass
many times.
2.Programmers can implement super-
classes called Abstract Classes.An
abstract class defines generic
behaviours.
An abstract superclass defines,and
may partially implement,the behav-
iour,but much of the class is unde-
fined and unimplemented.Other
programmers fill in the details with
specialised subclasses.