0% found this document useful (0 votes)
37 views63 pages

15CSE202 Object Oriented Programming

This document discusses a lecture on object oriented programming concepts. It covers key topics like abstraction, encapsulation, inheritance, and the lifecycle of an object. The lecture explains that classes group objects with common structure and behavior, and uses examples like a garden and real world objects to illustrate object oriented concepts. It also outlines the components of a class including private data, public interfaces, and special functions like constructors and destructors.

Uploaded by

ajey muthiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views63 pages

15CSE202 Object Oriented Programming

This document discusses a lecture on object oriented programming concepts. It covers key topics like abstraction, encapsulation, inheritance, and the lifecycle of an object. The lecture explains that classes group objects with common structure and behavior, and uses examples like a garden and real world objects to illustrate object oriented concepts. It also outlines the components of a class including private data, public interfaces, and special functions like constructors and destructors.

Uploaded by

ajey muthiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 63

15CSE202 Object oriented Programming

15CSE202 Object Oriented Programming


Lecture 2

Object Oriented Concepts

Nalinadevi Kadiresan
CSE Dept.
Amrita School of Engg.
15CSE202 Object oriented Programming 2

Roadmap

 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 3

Abstraction - Modelling

Abstraction focuses upon the essential


characteristics of some object; relative
to the perspective of the viewer.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 4

Abstraction focuses upon the essential characteristics of some object;


relative
June to the perspective of the viewer.
2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 5

Problems with Procedural Languages


struct stack struct queue
{ int top; { int front;
char* store;} ; int rear;
char* store;} ;
push(); insert();

pop(); delete();

All functions are global!!


June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 6

Problems with Procedural Languages

struct horse struct eagle


{weight, color, { age, weight
Age;}; wingspan;} ;

gallop(); fly();

canter(); hunt();

All functions are global!!


June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 7

Better Abstraction using OO Language


class stack class queue
{ int front;
{ int top; int rear;
char* store; char* store;}
push(); insert();
pop(); }; delete(); };

Class = State+ Behaviour


June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 8

Better Abstraction using OO Languages

class Horse class Eagle


{int weight; { int age;
String color; int weight
int age; int wingspan;

gallop(); fly();
canter(); }; hunt(); };

Class = State+ Behaviour


June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 9

Class = State+ Behaviour


 State
data members
fields
properties

 Behaviour
member functions
methods
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 10

Objects

An object has state, exhibits some well


defined behaviour, and has a unique
identity.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 11

An object has state, exhibits some well defined behaviour, and has a unique identity.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 12

Class

A class represents a set of objects


that share a common structure and
a common behaviour.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 13

A class represents a set of objects that share a common


structure and a common behaviour.
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 14

Objects in Real World

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 15

Objects in Real World

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 16

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 17

Roadmap
 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 18

Problems with my Public Garden

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 19

Solution 1: Build a high fence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 20

Write functions to Safeguard Data


0 ≤ x ≤ 100
Data Request to
Write
int x; change X
Function
Private!
Hidden !

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 21

Write function Code


void modify_x(int newval)
{
If(newval>100 || newval <0)
{PRINT ERROR AND EXIT}
else
x = newval;
}
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 22

Read Function Code

int read_x()
{
return x;
}

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 23

Encapsulation
 FIRST LAW OF OOP: Data must be hidden,
i.e.,PRIVATE
 Read access through read functions
 Write access through write functions
 For every piece of data, 4 possibilities
>>read and write allowed
>>read only
>>write only
>>no access
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 24

Encapsulation
Encapsulation hides the details
of the implementation of an
object.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 25

Encapsulation hides the details of the implementation of an


June 2019 Nalinadevi Kadiresan
object.
15CSE202 Object oriented Programming 26

Problem: Garden has only weeds

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 27

Initialization of Objects

 Special Functions – CONSTRUCTORS


ensure correct initialization of all data,
automatically called at the time of object
creation.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 28

Resource Deallocation
 Special Functions – DESTRUCTORS
ensure correct initialization of all
resources before an object “dies”
(goes out of scope).

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 29

Lifecycle of an Object
Born healthy
using constructors
 Lives Safely
using read/write functions
 Dies cleanly
using destructors

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 30

Lifecycle of an Object
 Born healthy
using constructors BRAHMA
 Lives Safely
using read/write functions VISHNU

 Dies cleanly
using destructors MAHESHWARA

HOLY TRINITY of Object Oriented Programming


June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 31

Anatomy of a Class
PRIVATE PUBLIC

data Read/write Public


Functions interface of
the class

Constructors
Destructors

Private ADT Functions


Functions run, fly, pop

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 32

Objects – public interface


Other objects can change the state of an object
by using only those methods that are exposed to
the outer world through a public interface. This
help in data security.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 33

Objects – Public Interface

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 34

Roadmap

 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 35

Code Reuse - Inheritance


 class Stack has functions pop and push
Can we add peek function without having the
source code with us?
 Extending the functionality of a class
OR
Specializing the functionality of a class
Stack

Stack 1
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 36

Subclasses - Inheritance

A subclass may inherit the structure and


behaviour of its superclass.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 37

A subclass may inherit the structure and behavior of


its superclass.
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 38

Inheritance

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 39

Inheritance Trees
Animal

Vertebrates Invertebrates

Mammal Fish Reptile Birds

Apes Monkeys Ungulates

is-a
relationship
Gorillas Chimpanzees Humans

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 40

Inheritance Trees

Employee

is-a
relationship
Manager

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 41

Nomenclature
BEST
Not recommended

Superclass Parent class Base class

Subclass Child class Derived class

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 42

Multiple Inheritance
Horse Eagle

Flying
Horse

One class having more than one base class

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 43

Graph Structure of Multiple Inheritance


A
Graphs have more
B C complicated structure than
trees.

F E D More difficult to maintain


and fix bugs etc.

G Ambiguity in function calls.

H
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 44

Inheritance Trees
Animal

Vertebrates Invertebrates

Mammal Fish Reptile Birds


Easier to maintain
and fix bugs
Apes Monkeys Ungulates
is-a
relationship
Gorillas Chimpanzees Humans
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 45

Ambiguity in Multiple Inheritance

Horse
Eagle
eat()
eat()

Flying Horse
eat()?

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 46

Overridding

• Redefining a
inherited method

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 47

Polymorphism

• Depends on the type of


object
• Eg. x.foo()

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 48

Roadmap

 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 49

Modularity
Modularity packages abstractions into discrete
units.

Classes
 Packages
 Domains

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 50

Modularity packages abstractions into discrete


units.
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 51

Roadmap

 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 52

Strong Typing
Strong typing prevents mixing abstractions.

X=Y

is allowed only if X and Y are objects of the same


class.
Weak typing: Allow some violations (e.g., in C)
Untyped: Dynamic Typing (LISP, javascript, etc.)

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 53

Strong typing prevents mixing abstractions.


June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 54

Roadmap

 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 55

Concurrency
Concurrency allows different objects to act at the
same time.

Java’s support of multithreading is a form of


concurrency.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 56

Concurrency allows different objects to act at the


June 2019 Nalinadevi Kadiresan
same time.
15CSE202 Object oriented Programming 57

Roadmap

 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 58

Persistence
Persistence saves the state and class of an object
across time and space, i.e., storage on permanent
storage media.

Object Serialization is supported in Java.

Can also use mapping to RDBMS using some


Object/Relational Mapping Scheme.

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 59

Persistence saves the state and class of an object across time


andJune 2019
space Nalinadevi Kadiresan
15CSE202 Object oriented Programming 60

Recap
 Abstraction
 Encapsulation
 Inheritance
 Modularity
 Strong Typing
 Concurrency
 Persistence

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 61

Classification of programming languages according


to its support of Object-Orientation

object-oriented programming Dynamic


+ Typed +
procedural employ some no other Multiparadigm
and data- of the basic structure or •Javascript
oriented imperative paradigm •Python
• C++ structures •Smalltak •Ruby
•Ada 95 •Java
•C#
June 2019 Nalinadevi Kadiresan
15CSE202 Object oriented Programming 62

Picture courtesy

CONCEPTS OF OBJECT ORIENTED


PROGRAMMING
By Grady Booch
By Ravi P Reddy

June 2019 Nalinadevi Kadiresan


15CSE202 Object oriented Programming 63

Next Session will be


Overview of Java

June 2019 Nalinadevi Kadiresan

You might also like