Most Inmportant ........ Oops Concept

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Object Oriented Programming

Approach

Session I

eACCP/Object Oriented Concepts and Java 2/Session 1/ 1 of 26


Session Objectives
 Explain structured programming and its
drawbacks
 Discuss Object Oriented Programming and its
advantages
 Define classes and objects
 Identify properties and methods
 Explain public and private sections of a class
 Discuss abstraction and encapsulation
 Describe inheritance and polymorphism

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 2 of 26


Structured Programming - I
mainprg()
{ Program is a set of tasks
Print “Hello World”
result = 13*12/2.5
Print “Result =” result
}
Broken down into
smaller and independent
functions

Structured programming follows a ‘top-down’ approach


sometimes termed as ‘step-wise refinement’.

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 3 of 26


Structured Programming - II
 A program is organised into a hierarchy of
modules
 Each module or function has a single entry
and exit point
 Three types of control flow:
 Sequential
 Test
 Iteration

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 4 of 26


Disadvantages of Structured
Programming
 Difficult to separate data from functions
that manipulate it
 Solutions are recreated and not reused
 Difficult to track and manage changes

Object oriented programming provides techniques for


managing complexity and reuse of software

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 5 of 26


The Object Oriented Approach -I
 Object oriented programming grew in the
70’s as a solution to the problems of
structured programming
 Models human thought process as closely
as possible
 Deals with data and procedures that operate
on data as a single ‘object’

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 6 of 26


The Object Oriented Approach - II
All around us in the real world are
objects.

Each object has certain


characteristics and exhibits
certain behaviour

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 7 of 26


The OO Approach - An Example
Top-Down approach OOP

We are going to build a hotel We are going to build a 10


storey hotel with ordinary,
deluxe and luxury suites
that also possesses an
executive lecture hall

We are going to then design We are going to build a


several floors, rooms and hotel along these
then the lecture hall specifications(components)

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 8 of 26


The OO Methodology
The four steps in this methodology are:
 Identify the problem

 Identify the objects needed for the solution

 Identify the messages to be sent to the

object
 Create a sequence of messages to the

objects that solve the problem

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 9 of 26


Object Oriented Programming
OOP (Object oriented Programming) offers a powerful
model for writing software. Here:
Data: Accounts Functions:
Code & Data
•No. of employees •Calculate salary
•Salary statements •Pay salary
•Bills •Pay bills
•Vouchers •Tally accounts
•Receipts •Transact with
Object •Petty cash records banks
•Banking data

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 10 of 26


Basic Object Oriented Concepts
 Object
 Helps to understand the real world
 Provides a practical basis for computer applications
 Class
 Describes a set of related objects
 Property
 A characteristic of an object – also called attribute
 Method
 An action performed by an object

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 11 of 26


Abstraction - I
 A process of examining certain aspects of a
problem as relevant to a particular
application

t e 1
bu
t ri
t
A bute 3
Attri
Method 1
Method
2
eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 12 of 26
Abstraction - II
Abstraction is classified into two
 Data abstraction

 Identifying the properties related to a particular


application
 Procedural abstraction
 Applying focus on the arguments and the return
value of the procedure rather than its
implementation

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 13 of 26


Inheritance
Animals
Inheritance is a
property that
allows reuse of
an existing class Insects Mammals Reptiles Amphibians

to build a new
class

Humans Non-Humans

Reusability can be
achieved through
inheritance

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 14 of 26


Reusability
Shape
Programs can be broken
into reusable objects
Existing classes can be
used with additional
features

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 15 of 26


Encapsulation
 A process of information hiding
 Selective hiding of data
 Prevents accidental tampering of data
 Errors are easier to isolate and fix

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 16 of 26


Polymorphism
 Same functions with different behaviour on
different classes
Class - Here, a method
Artiste method perform()
implemented on the
subclasses would
Dancer Sculptor Poet give different results

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 17 of 26


More on Classes
 A class contains
 Data members
 Functions
 Data members are accessed through the
functions
 An object is an instance of a class
 A class can have sections which are not
accessible to other classes

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 18 of 26


A Class Definition
class Animal
{
public int noOfLegs;
public String name;
private char gender;
 
public void showData()
{
display(“Name :” + name);
display(“Number of Legs:” + noOfLegs );
display(“Gender :” + gender);
}
} eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 19 of 26
Access Specifiers of a Class
Class

Not
accessible Private
from outside
the class Data or
functions
Public
Accessible
from outside
the class Data or
functions

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 20 of 26


Member Function
 Is a message to an object
 Is usually declared public
 Should not have the same name as a data
member
 Provides access to the data members of a
class

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 21 of 26


Using the Class
 Instantiating a class
 Animal anim1=new Animal();
 Assigning values
 anim1.name = “zebra”;
 Calling member functions
 anim1.showData();

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 22 of 26


Passing and Returning Objects
c t
bje Compiler
o

COMPILER
creates a
Function FORMAL
variable in
Formal
variables the called
function

All data members of the object are copied to the


formal variable and they can be assigned any value
eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 23 of 26
Object Oriented Languages
 Some of the leading object oriented
languages are:
 C++
 Smalltalk
 Eiffel
 CLOS
 Java

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 24 of 26


What is Java ?
Java is a high level programming
language introduced by Sun
Microsystems in June 1995

It was developed by a team under


James Gosling
James Gosling

It provides for interactive processing and for the use of


graphics and animation on the Internet.
eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 25 of 26
Features of Java
 Simple
 Object oriented
 Platform independent
 Robust
 Secure
 Distributed
 Multithreaded
 Dynamic

eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 26 of 26

You might also like