object oriented programmingcourses.daiict.ac.in/.../content/0/oop.pdf · • abstraction strategy...

24
Object Oriented Programming [email protected]

Upload: others

Post on 13-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Object Oriented Programming

[email protected]

Page 2: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Recall Terms

• Class

• Instance/Object

• Operation

• Method

• Message sending

Page 3: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Class

• It is an Object Type

• Specifies/describes objects of similar behavior

• Is an Implementation construct in OOP languages for creating abstract data types

Page 4: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Object

• Instance of a class

• A run-time instance of a class

• An instance of a class, is an object that belongs to that class

Page 5: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Operation/Method

• Operations that can be performed on objects define behavior of an object

• Class developer needs to provide Implementation of operations

• Java calls them Methods, while C++ calls them member functions

Page 6: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Messages Sending

• In order to get some services of an object, you send them messages

• For, Exampleday.getYear()

books.push( abook );

• day and books are message recipient objects, they would perform some operation in order to serve the client’s request

• Message Sending does not tell, what operation has to be performed in order to serve the request, object determines it – operation binding

Page 7: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

What is Object Oriented Programming?

• Different paradigm of Abstraction

– A system is viewed as community of objects contributing toward the goal

• And, there are some more characteristics– Inheritance, and– Polymorphism

Page 8: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Case Study : Banking System

• Suppose you want automate saving account system of a bank-branch.

• Each account, has following information-– Account No, Account Holder Name, Balance

• Following Bank branch operations– Open an account– Locate an account– Deposit money into a account– Withdraw Money From Account– Close an account

Page 9: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Think: How do we program it in C?

• What Data Structure do we use?

• What Functions do we have and what are their prototypes?

Page 10: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Procedure Oriented Programming

• Has been strategy for quiet a log time for major real life applications

• Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

• Function here may not be exactly function as construct in programming language like C, roughly, function here is a sub-task

• The abstraction strategy is decompose tasks based on functional responsibilities

Page 11: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

First Level Data Flow Diagram a formal Diagram: called 0-level DFD

Page 12: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Banking System : DFD Level-1

Page 13: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Implementation in C

Global Data

Do all functions access data that they are supposed to?

Accounts.c

Page 14: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Implementation in C

Accounts.h

Page 15: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

main functionbank.c

Page 16: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Some Issue Raised

• Though C module concept, provides some kind of encapsulation by making bank data private within Accounts module- note the use of static keyword with account array– Provides implementation independence, you can

change implementation from static array of accounts to dynamic array or search tree for improving the search

• Still there are some issues– One, what happens when more type of accounts, like

current or loan accounts are added?– Two, how about extending this program to work for

multiple branches, and we provide any-where banking?

Page 17: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Why these issues concerns us

• In addition to complexity, following are concerns these issues address-

• First one relates to reusability and maintainability,

• While second relates to Instantiation

Page 18: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

First issue relates to reusability and maintainability

• Issue One: What happens when more type of accounts are added?– Probably you would create another module let us say

CurrentAccount.c, by copy and paste as most of functions remain same

• Similar logic (many business rules are common) has to be coded and maintained at multiple places– a reusability and maintainability issue

Page 19: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Scond issue relates to Instantiation

• Issue Two: how about extending this program to work for multiple branches, and we provide any-where banking?

• Probably you would make the accounts array as two dimensional to store data of multiple branches, in that case, all implementation code has to be changed to access data from different data structure; changing a tested and live program is a nightmare

• A serious concern?• Probably, we would able to find a far better solution if we

could instantiate module account.c, let us call it as branch, and then have an array or list of branchmodule

Page 20: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Solution is not easy in C or procedure oriented approach?

• Things become complex.

• OOP provides, much elegant solution– Community of Objects, that are instances of classes,

given next

• We shall talk about derivation of these classes some time later

Page 21: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

OO Solution

Page 22: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Implementation View of Object Oriented Programming

Consider:Bank Class, Branch Class, and Account Class

Page 23: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Implementation ViewProcedure Oriented Programming

Page 24: Object Oriented Programmingcourses.daiict.ac.in/.../content/0/OOP.pdf · • Abstraction Strategy here is : A set of functions (procedures) are going to accomplish the desired task

Thanks