Nothing Special   »   [go: up one dir, main page]

Object Oriented Programming Methodology Using Java: Prof: Deepak Gaikar (Computer Department) RGIT-Mumbai

Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

Object Oriented Programming Methodology

Using Java

Prof: Deepak Gaikar


(Computer Department)
RGIT-Mumbai
Fundamental concepts of object
oriented programming
Syllabus
Module Unit Topic Hrs
No. No.
1 Fundamental concepts of object oriented programming 4
1.1 Overview of programming
1.2 Introduction to the principles of object-oriented programming:
classes, objects, messages, abstraction, encapsulation, inheritance,
polymorphism, exception handling, and object-oriented containers
1.3 Differences and similarity between POP and OOP
Overview of programming
• Programming: programming language is
artificial human created language which
translates instruction from human redable
to computer readable format.
• Programming is art of solving
computational problems by.
• To solve these problems which are
normally written in programming language.
Concepts of Procedure oriented
programming
Procedure Oriented Programming(POP)

• Programming languages such as like


COBOL ,FORTRAN,C (using high level
languages) were based on Procedural
Oriented Programming(POP) language.

• POP language program uses branching


statements and procedures to execute one or
other set of instructions, depending on the
result that you want.

• Procedure also known as a routine or


subroutine or a function.
Procedure-Oriented Programming
• Procedure-oriented programming basically
consists of writing a list of instructions for
the computer to follow and organizing these
instructions into groups known as functions.
• When you write large programs in Procedure
Language ,they are complex to understand
and hard to program.
Note:Procedural programming is a programming
paradigm in which a program is composed of one
or more procedures which are invoked in a
particular order to achieve the desired result. Every
procedure is a set of instructions representing a
specific task.
Note:
• In structured programming(like c) we can
overcome this problems by dividing a
Large program into different functions.
• But it gives birth to other problem.
• Structured programming is any
programming when functionality is divided
into units like for loop, while loop, if...
then etc block structure.
Also, here the a piece of code (function)
can be re-used.
Typical structure of procedure-oriented program
Global data
Main function
Local data

Function-1 Function-2 Function-3

Function-4 Function-5

Function-6 Function-7 Function-8


Characteristics of Procedure-
Oriented Programming
• Large programs are divided into smaller
programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from
function to function.
• Functions transform data from one form to
another.
• Employs top-down approach in program
design.
Relationship of data and functions in
procedural programming

Global Data Global Data

Function-1 Function-2 Function-3

Local Data Local Data Local Data


Problem of Procedure oriented
programming?
• In procedural language main problem are
The functions have unrestricted access to
global data.
• No security for data. If we declare a variable
before main function then it can be accessed
freely from any function present in the
program.
• No better memory management.
• Difficult to implement today's client
requirements
Problem of Procedure oriented
programming?
• No structure or code reusability. Hence time
of development, testing and length of
program increases.
• As length of application increases it causes
slow performance.
• Code maintenance and enhancements are
difficult.
• No proper way method for Exception
handling (error handling).
Some other Problem of Procedure
oriented programming?
• Computer languages generally have built-in
data type : interger,character,float etc.
When we try to invent a new data type or a
user-define data type. Then it become very
default to create it.
• e.g. we work with dates or complex no. then it
becomes very difficult to work with built in
types .
• Crating your own data types is a feature called
of extensibility.
• Procedure oriented language are not extensible
Procedure-Oriented Programming
This problem is overcome by with the
development of new programming technique
known as object oriented programming(OOP).
Procedure-Oriented Programming
• POP the emphasis is on functions rather than
data, and data is also not secured in this
approach.
• The OOP approach eliminates these
weakness of POP by combining the data and
the function that work on it into a single unit
called class.
• So the data is secured from the outside world
• OOP is implemented on the real-world
entities called objects that have some
characteristics and behavior,
Fundamental concepts of object
oriented programming
Contents
1. What is OOP?
2. Classes and Objects
3. Basic Concepts of OOP
Inheritance
Abstraction
Encapsulation
Polymorphism
What is OOP?
• Object Oriented Programming(OOP) is an
Engineering approach for building software
systems.
• Based on the concepts of classes and object
that are used for modeling the real world
entities.
Object-Oriented Programs
- Consists of group of co-operating objects.
- Object exchange messages, for the purpose
of achieving a common objective.
- Implemented in object-oriented languages.
What is OOP?
 Better suited for….team development.
 Facilitated utilizing and creating reusable
software components.
 Easier for GUI Programming
 Easier for Software maintenance
 All modern language are object oriented:
Java, C#, PHP, Perl, C++,...…..
Real world object
• In physical world we deal with object like
person or car.
• Such object not like data and functions.
• In complex real world situations we have
objects which have some attributes &
behavior.
Attributes :
• Every object has some attribute different
types of objects contain different attributes
or characteristics.
• Example :
The student are name,roll number,subject
The attribute for car whould be color,
engine power,number of seates etc.
• Attribute are real world are like data they
have some specific value Raj(for name) or
23 (roll number).
Behavior
• Behavior actually determines the way an
objects interacts with other objects.
• Like function to do some task.

• e.g if manager orders an employee to do


some task then he responds either by doing
it or not doing it.
OOP …Object
continue …

• In Object Oriented Programming deals with


similar objects.

• The data of an object can be accessed only


by the functions associated with that object.

• Functions of one object can access the


functions of another objects.
Organization of data and functions in OOP

Object A Object B

Data Data

Communication
Functions Functions

Object C

Data

Functions
Basic Concepts of Object-Oriented
Programming
• Objects
• Classes
• Data Abstraction and Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
Objects in OOP
• Object are the basic run-time entities in an
object-oriented system.
• They may represent a person, a place ,
a bank account, a table of data or
any item that the program has to handle.
• Objects take up space in the memory and
have an associated address like a structure
in C.
• When a program is executed, the objects
interact by sending messages to one another
OBJECT

HAS DOES

color bounce
diameter roll
brand

properties behavior
Objects examplein OOP
If there are two objects customer and account
then the customer object may send a message to
account object ..requesting for the bank balance.
Object : CUSTOMER Object :ACCOUNT

DATA DATA
AC No. AC No.
Name of AC Holder AC Balance
Address Type of Account

FUNCTIONS FUNCTIONS
Deposit Account Balance
Withdrawal
AC Balance Display
Classes
• A class is user-defined data type which contains
data members and member functions...Members
function operate those data Members.
• Classes are user defined data types and behaves
like the built in type of a programming language.
• Once a class has been defined, we can create any
number of objects belonging to that class. Thus a
class is collection of objects of similar type.
• class is collection of similar kind of objects.
Classes :The entire set of data & code of an object can be
made a user-defined data type with the help of a class. Objects
are actually variable of the type class

OBJECT

HAS DOES

color bounce
diameter roll
brand

properties behavior
Classes
• class is a collection of fields (data) and
methods (procedure or function) that
operate on that data.
• Class are just like the blueprints of objects
that model real world things.
• one of advantage of using classes is that
once you have created a class for a certain
type of object you can reuse that class in any
project.
Pr
of.
D
ee
pa
k
Ga
ika
r
A class Is defined by 3 element
1.A unique class name
2.Data members or attributes
3.Member functions or methods

circlecle

centre
radius
circumference()
circumference()
area()
Circle

centre
radius

circumference()
area()

Prof.Deepak Gaikar
rubaim.khan.kotye@ericsson.com
A class describes an object

OBJECT

HAS DOES

color bounce
diameter roll
brand

properties behavior
Prof.Deepak Gaikar
a class describes an object
Class ball

String color;
float diameter;
String brand;
OBJECT
void bounce(float h)
void roll (float d)

HAS DOES

color bounce
diameter roll
brand

properties behavior
Prof.Deepak Gaikar
Circle ball

String color;
Data members
Float diameter;
String brand;

void bounce(float h) Members methods


void roll (float d)

Prof.Deepak Gaikar
The syntax for defining class in c++ :
class class_name
{
private :
variable declarations;
functions declarations;
public:
variable declarations;
functions declarations;
};
void main()
{
class_name variabale_name;
….
}
The syntax for defining class in java :
class class_name
{
private
variable declarations;
functions declarations;
public
variable declarations;
functions declarations;
}
class mainclass_name
{
public static void main(String args[])
{
class_name variabale_name=new class_name( );
….
}
}
example of class in Java
class ball class ballmain
{ {
String color; public static void main (String args[ ])
float diameter; {
String brand; // Following statement would
void bounce(float h) create an object
{ } ball b=new ball( );
void roll (float d) }
{ } }
}
Pr
of.
D
ee
pa
k
Ga
ika
r
Pr
of.
D
ee
pa
k
Ga
ika
r
Pr
of.
D
ee
pa
k
Ga
ika
r
Pr
of.
D
ee
pa
k
Ga
ika
r
Pr
of.
D
ee
pa
k
Ga
ika
r
Ball b =new Ball();
b.color=“white”;
b.diam=2.0f;
b.brand=“nike”; Pr
of.
D
b.bounce(); ee
b.boll(); pa
k
Ga
ika
r
Pr
of.
D
ee
pa
k
Ga
ika
r
Pr
of.
D
ee
pa
k
Ga
ika
r
Pr
of.
D
ee
pa
k
Ga
ika
r
OBJECT

Properties Behavior
i.e data member i.e method

Pr
of.
D
ee
pa
k
Ga
ika
r
Basic Concepts of OOP
Data Abstraction and Encapsulation continue …
oThe wrapping up of data and functions into a
single unit is known as encapsulation.

oThe data is not accessible to the outside world,


and only those functions which are wrapped in the
class can access it.

oThese functions provide the interface between


the object’s data and the program. This insulation
of the data from direct access by the program is
called data hiding or information hiding.
Data Abstraction & Encapsulation
o The attributes wrapped in the classes are
called data members and the functions that
operate on these data are called methods or
member functions.
o Since the classes use the concept of data
abstraction, they are known as Abstracted
Data Types (ADT).
Basic Concepts of OOP
Data Abstraction :
The concept of abstraction is closely related
to the concept of data or information hiding
abstraction separates qualities of data type
& the associated implementation detailed.
this helps the properties of the abstract
data types user interface while the
implementation detailed remain hidden.
Inheritance
o Inheritance is the process by which objects of one
class acquire the properties of objects of another
continue …

class.
o It supports the concept of hierarchical
classification.
o Inheritance is a compile-time mechanism in Java
that allows you to extend a class (called the
baseclass or superclass) \ with another class
(called the derived class or subclass) or
o The mechanism of deriving a class from another
existing class is called as inheritance .
o Access specifies:- public, protected ,private,
default. 
Property Inheritance

Bird
Attributes:
Feathers
Lay eggs

Flying Bird Non-flying Bird

Attributes: Attributes:
------------ ------------
------------ ------------

Robin Swallow Penguin Kiwi


Attributes: Attributes: Attributes: Attributes:
------------ ------------ ------------ ------------
------------ ------------ ------------ ------------
Derived class-base class declaration syntax in Inheritance :
class A
{
……………
……………..
}
class B extends A
{
…………..
……………
}
class c extends B
{
……………
…………… }
Inheritance :
continue …

o Inheritance provides the idea of


reusability.

oWe can add additional features to an existing


class without modifying it.
(By deriving new class from existing one. The
new class will have the combined features of both
the classes.)
Basic Concepts of OOP
continue …

Polymorphism:ability to take more than one form

o An operation may exhibit different behaviors


in different instances.
o The behaviour depends upon the types of
data used in the operation.
o add( 3, 5) gives 8.
o Add(“hello”, “-world”) gives “hello-world”.
Polymorphism
o ability to take more than one form

oThe process of making an operator to exhibit different


behaviors in different instances is known as operator
overloading.
<< Insertion Operator
<< Left-shift bit-wise operator

oUsing a single function name to perform different types of


tasks is known as function overloading.
add( 3, 5) gives 8
Add(“hello”, “-world”) gives “hello-world”
Dynamic Binding
continue …
o Binding refers to the linking of a procedure call
to the code to be executed in response to the
call.
o Dynamic binding (late binding ) means that the
code associated with a given procedure call is
not known until the time of the call at run-
time.It is associated with polymorphism &
inheritance.
Message Passing
continue …

• An oop consists of a set of objects that


communicate with each other.
• oop involves the following steps:
o Creating classes that define objects and their behaviour.
o Creating objects from class definitions.
o Establishing communication among objects.
•Objects communicate with one another by
sending and receiving information.
Basic Concepts of OOP
continue …

Message Passing
o A message for an object is a request for
execution of a procedure.
o The receiving object will invoke a function
and generates results.
o Message passing involves :
o The name of the Object.
o The name of the Function.
o The information to be send.
Benefits of OOP
• Inheritance – eliminate redundant code and
extend the use of existing classes.
• We can build programs from the standard
working module.
• Data hiding helps the programmer to build
secure programs that can not be invade by code
in other parts of the program.
• Multiple instances of an objects can co-exists
with out any interference.
Benefits of OOP
• It is easy to partition the work in a
project based on objects.
• Object-oriented system can be easily
upgraded from small to large systems.
• Message passing techniques for
communication between objects makes
the interface descriptions with external
systems much simpler.
• Software complexity can be easily
managed.
Characteristics of Object-Oriented Programming
• Importance is on data.
• Programs are divided into objects.
• Objects may communicate with each other
through functions.
• Data structures are designed such that they
characterize the objects.
• Functions that operate on the data of an object
are tied together in the data structure.
• Data is hidden and can not be accessed by
external functions.
• New data and functions can be added easily
whenever necessary.
Difference Between Procedure Oriented Programming
(POP) & Object Oriented Programming (OOP)
Procedure Oriented Object Oriented
Programming Programming

In POP, program is divided


In OOP, program is divided into
Divided Into into small parts
parts called objects.
called functions.

In POP,Importance is not In OOP, Importance is given to


given to data but to functions the data rather than procedures
Importance
as well as sequence of actions or functions because it works as
to be done. real world.

POP follows Top Down OOP follows Bottom Up


Approach
approach. approach.
Procedure Oriented Programming Object Oriented Programming

Access POP does not have any access OOP has access specifies named
Specifiers specifier. Public, Private, Protected, etc.

In POP, Data can move freely In OOP, objects can move and
Data Moving from function to function in the communicate with each other
system. through member functions.

To add new data and function in OOP provides an easy way to


Expansion
POP is not so easy. add new data and function.
Procedure Oriented Object Oriented
Programming Programming

In OOP, data can not move


In POP, Most function uses
easily from function to
Global data for sharing that can
Data Access function,it can be kept
be accessed freely from function
public or private so we can
to function in the system.
control the access of data.

POP does not have any proper


OOP provides Data Hiding
Data Hiding way for hiding data so it is less
so provides more security.
secure.

In OOP, overloading is
In POP, Overloading is not possible in the form of
Overloading
possible. Function Overloading and
Operator Overloading.

Example of POP are : C, VB, Example of OOP are : C++,


Examples
FORTRAN, Pascal. JAVA, VB.NET, C#.NET.

You might also like