Вы находитесь на странице: 1из 35

LECTURE-1

INTRODUCTION
TO
OBJECT ORIENTED
PROGRAMMING SYSTEM(OOPS)

Objectives:

Know the difference between procedural programming


and OOP
Know basic terminology in OOP
Know the importance of OOP
Know four design principles of OOP
Know about OOP programming languages

PROCEDURAL PROGRAMMING
As the name suggests, focus is on Procedures.
Programs written in Procedural programming

language consist of functions.


These functions are interdependent, hence difficult
to separate.
Less reusability of code. program development
becomes a complex task.
Program and data are separated from each other.
Follow structured programming paradigm.

Structured programming
MAIN PROGRAM

FUNCTIO

FUNCTION

N1

FUNCTION

FUNCTION

GLOBAL DATA

FUNCTION

PROCEDURAL PROGRAMMING
As seen from figure:
Functions are interdependent
They share global data
Hence:
Procedures/functions are often hard to reuse.
programs are often hard to extend and maintain.
More maintenance cost.
Less data security.

Object Oriented Programming(OOP)


Program is broken into independent chunks called objects.

OOP enables us to consider each real world entity as object.


These objects can be integrated to form a complete program.
Some terminology

object - usually a person, place or thing (a noun)


method - an action performed by an object (a verb)
type or class - a category of similar objects (such as

automobiles)

Objects have both data and methods.

Key idea is Object oriented

OOP
Object 2

Object 1

Data

Data

Function

Function

Object 3

Data
Function

Advantages of OOP

Objects can be reused across various programs.


Cleaner code in short duration.
More data security.
More close to real world thus easier to understand.
More manageable.
Easier debugging
classes can be tested independently
reused objects have already been tested

Difference Between
Procedure Oriented Programming (POP)
&
Object Oriented Programming (OOP)

Object Oriented Programming


Basic terminologies

Object oriented Programming defined

Object-oriented programming (OOP) is a programming


paradigm that represents concepts as "objects" that
have data fields (attributes that describe the object) and
associated procedures known as methods. Objects,
which are usually instances of classes, are used to
interact with one another to design applications and
computer programs.

Basic terminology
14

object
- usually a person, place or thing (a noun)
method
- an action performed by an object (a verb)
attribute
- description of objects in a class
class
- a category of similar objects (such as automobiles)
- does not hold any values of the objects attributes

What Is an Object?
Informally, an object represents an entity, either

physical, conceptual, or software.

Physical entity

Truck

Conceptual entity
Chemical
Process

Software entity
Linked List

A More Formal Definition


An object is an entity with a

Attributes

well-defined boundary and


identity that encapsulates
state and behavior.
State

is represented by
attributes
and
relationships.
Behavior is represented by
operations, methods, and
state machines.

Object
Operations

An Object Has State


The state of an object is one of the possible conditions in

which an object may exist.


The state of an object normally changes over time.

Professor Clark
Name: J Clark
Employee ID: 567138
Date Hired: July 25, 1991
Status: Tenured
Discipline: Finance
Maximum Course Load: 16 classes

Name: J Clark
Employee ID: 567138
HireDate: 07/25/1991
Status: Tenured
Discipline: Finance
MaxLoad: 3

Professor Clark

An Object Has Behavior


Behavior determines how an object acts and reacts.
The visible behavior of an object is modeled by the set of

messages it can respond to (operations the object can


perform).

Professor Clarks behavior


Submit Final Grades
Accept Course Offering
Take Sabbatical
Maximum Course Load: 3 classes

Su
bm

Professor Clark

itF
in

al
G
ra

de
s(

Professor Clark

Se
tM
ax
Lo
ad
()

A
() cce
pt
Co
u

rs
eO
ffe
rin
g

An Object Has Identity


Each object has a unique identity, even if the state is

identical to that of another object.

Professor J Clark
teaches Biology

Professor J Clark
teaches Biology

What Is a Class?
A class is a description of a set of objects that share the same

properties and behavior.


An object is an instance of a class.
Class: Professor

Objects

Attributes

- name
- employeeID : UniqueId
- hireDate
- status
- discipline
- maxLoad

Professor Smith

+ submitFinalGrade()
+ acceptCourseOffering()
+ setMaxLoad()
+ takeSabbatical()

Professor Mellon
Professor Jones

Professor

Operations

What Is an Attribute?
An attribute is a named property of a class that describes a

range of values that instances of the property may hold.


A class may have any number of attributes or no attributes
at all.
Student
Attributes

- name
- address
- studentID
- dateOfBirth

Attributes in Classes and Objects


Class

Student

name: M. Modano
address: 123 Main
studentID: 9
dateofBirth:
03/10/1967

Objects

- name
- address
- studentID
- dateOfBirth
name: D. Hatcher
address: 456 Oak
studentID: 2
dateofBirth: 12/11/1969

What Is an Operation?
An operation is the implementation of a service that can

be requested from any object of the class to affect


behavior.
A class may have any number of operations or none at
all.
Student

Operations

+ get tuition()
+ add schedule()
+ get schedule()
+ delete schedule()
+ has pre-requisites()

Example: class Professor


class Professor {
private String name;
private int age;
private String speciality;

public Professor (String sm, int ia, String ss) {


name = sm;
age = ia;
speciality = sst;
}
public String getName () { return name;}
public int getAge () { return age;}
public String getSpeciality () {
return speciality;}

Professor
- name : String
- age : int
- speciality : String
+getName() : String
+getAge() : int
+getSpeciality() : String

Example : Instance of Professor


wang : Professor

name = wang

age = 35
speciality = computer

Professor wang = new Professor (wang,


35,computer);

Basic Principles of Object Orientation

Polymorphism

Inheritance

Encapsulation

Abstraction

Object Orientation

Abstraction
"To represent the essential feature without representing the

background details."
lets you focus on what the object does instead of how it does it.
provides you a generalized view of your classes or object by
providing relevant information.
process of hiding the working style of an object, and showing
the information of an object in an understandable manner.
Analogy:
When you drive a car, you dont have to know how the
gasoline and air are mixed and ignited.
Instead you only have to know how to use the controls.

Example
Real World Example of Abstraction
Suppose you have an object Mobile Phone.
Suppose you have 3 mobile phones as follows:
Nokia 1400 (Features:- Calling, SMS)
Nokia 2700 (Features:- Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:-Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading

E-mails)
Abstract information (Necessary and Common Information)

make a call to any number


send SMS.

abstract class MobilePhone


{ public void Calling();
public void SendSMS();
}

Encapsulation
Wrapping up data member and method together into a

single unit (i.e. Class) is called Encapsulation.


Encapsulation is a technique used to protect the
information in an object from the other object.
Only objects methods can modify information in the
object.
Also known as information hiding.
one may think of information hiding as being the
principle and encapsulation being the technique.

Polymorphism
Polymorphismthe same word or phrase can be mean

different things in different contexts


Analogy: in English, bank can mean side of a river or a
place to put money
There can be two functions called output.
Each output method would do the right thing for the
type of argument it has.
One output might display a number whereas a different
one might display a name.

Example: Polymorphism
Get Current Value

get
Cu
rre

Stock

ntV
a lu
e()

get
Cu
rre

Bond

ntV
a lu
e()

get
Cu
rre

ntV
a lu
e()

Mutual Fund

In this example, a requesting object would like to know the current value of a financial instrument. However,
the current value for each financial instrument is calculated in a different fashion. The stock needs to
determine the current asking price in the financial market that it is listed under. The bond needs to determine
the time to maturity and interest rates. A mutual fund needs to look up the closing price from the day from
the fund management company.
In a non object-oriented development environment, we would write code that may look something like this:
IF financialInstrument = Stock THEN
calcStockValue()
IF financialInstrument = Bond THEN
calcBondValue()
IF financialInstrument = MutualFund THEN
calcMutualFundValue()
With object technology, each financial instrument can be represented by a class, and each class would know
how to calculate its own value. The requesting object simply needs to ask the specific object (for example,
Stock) to get its current value. The requesting object does not need to keep track of three different operation
signatures. It only needs to know one.
Polymorphism allows the same message to be handled in different ways depending on the object that
receives it.

Inheritance
Is the process by which new class is created from existing

class.
Derived classes have all the features of the base class in
addition to some new features.
The new inheriting class is called derived or sub class and
inherited class is called base or super class.
Term inheritance comes from inheritance of traits like eye
color, hair color, and so on.
Is an is a kind of relationship.

An Inheritance Hierarchy
Vehicle

Automobile

Sedan

Motorcycle

Sports Car

Bus

School Bus

Luxury Bus

Review: Introduction to Object Orientation

State 2 differences between functional

programming and OOP.


What are the four basic principles of object
orientation? Provide a brief description of each.
What is an Object and what is a Class? What is the
difference between them?
What is an Attribute?
What is an Operation?
Describe the strengths of object orientation.

Вам также может понравиться