2. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 2 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
3. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 3 of 26
Structured Programming - I
mainprg()
{
Print “Hello World”
result = 13*12/2.5
Print “Result =” result
}
Program is a set of tasks
Broken down into
smaller and independent
functions
Structured programming follows a ‘top-down’ approach
sometimes termed as ‘step-wise refinement’.
4. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 4 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
5. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 5 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
6. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 6 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’
7. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 7 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
8. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 8 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
several floors, rooms and
then the lecture hall
We are going to build a
hotel along these
specifications(components)
9. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 9 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
10. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 10 of 26
Object Oriented Programming
OOP (Object oriented Programming) offers a powerful
model for writing software. Here:
Accounts
Data:
•No. of employees
•Salary statements
•Bills
•Vouchers
•Receipts
•Petty cash records
•Banking data
Functions:
•Calculate salary
•Pay salary
•Pay bills
•Tally accounts
•Transact with
banks
Code & Data
Object
11. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 11 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
12. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 12 of 26
Abstraction - I
A process of examining certain aspects of a
problem as relevant to a particular
application
Method 1
13. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 13 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
14. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 14 of 26
Inheritance
Inheritance is a
property that
allows reuse of
an existing
class to build
a new class
Animals
Reusability can be
achieved through
inheritance
Insects Mammals Reptiles Amphibians
Humans Non-Humans
15. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 15 of 26
Reusability
Programs can be broken
into reusable objects
Existing classes can be
used with additional
features
Shape
16. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 16 of 26
Encapsulation
A process of information hiding
Selective hiding of data
Prevents accidental tampering of data
Errors are easier to isolate and fix
17. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 17 of 26
Polymorphism
Same functions with different behaviour on
different classes
Class -
Artiste
Dancer Poet
Sculptor
Here, a method
method perform()
implemented on the
subclasses would
give different results
18. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 18 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
19. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 19 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);
}
}
20. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 20 of 26
Access Specifiers of a Class
Class
Data or
functions
Data or
functions
Private
Public
Not
accessible
from outside
the class
Accessible
from outside
the class
21. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 21 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
22. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 22 of 26
Using the Class
Instantiating a class
Animal anim1=new Animal();
Assigning values
anim1.name = “zebra”;
Calling member functions
anim1.showData();
23. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 23 of 26
Passing and Returning Objects
Compiler
creates a
FORMAL
variable in
the called
function
All data members of the object are copied to the
formal variable and they can be assigned any value
Formal
variables
Function
24. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 24 of 26
Object Oriented Languages
Some of the leading object oriented
languages are:
C++
Smalltalk
Eiffel
CLOS
Java
25. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 25 of 26
What is Java ?
Java is a high level programming
language introduced by Sun
Microsystems in June 1995
It provides for interactive processing and for the use of
graphics and animation on the Internet.
James Gosling
It was developed by a team under
James Gosling
26. eACCP/ Object Oriented Concepts and Java 2/ Session 1/ 26 of 26
Features of Java
Simple
Object oriented
Platform independent
Robust
Secure
Distributed
Multithreaded
Dynamic