59
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING Lecture slides by: Farhan Amjad A new paradigm of programming

Introduction to object oriented language

Embed Size (px)

Citation preview

Page 1: Introduction to object oriented language

INTRODUCTION TO OBJECT ORIENTED

PROGRAMMING

Lecture slides by:

Farhan AmjadA new paradigm of programming

Page 2: Introduction to object oriented language

LEARNING OUTCOMES After this lecture, you should be able to

Difference of OO technique by other techniques

Understand the meaning of objectKnow the difference between an object and

a classKnow the concepts of object orientationUnderstand how objects communicate

Page 3: Introduction to object oriented language

OOPS

It’s OOP not OOPS

Page 4: Introduction to object oriented language

COURSE OUTLINE Detailed Course Outline is on the subject homepage

http://groups.google.com/group/sp09_bcs2

Check your email often for announcements related to the course

Page 5: Introduction to object oriented language

RESOURCES Recommended Books:

OOP using C++ 6th Edition, Prentice Hall Reference Books:

OOP in C++ by Robert Lafore Thinking in C++, 2nd Edition, by Bruce Eckel

Page 6: Introduction to object oriented language

FIVE TIPS TO SUCCESS

Page 7: Introduction to object oriented language

WORK HARD

Page 8: Introduction to object oriented language

TRY MORE EXERCISES AND MORE PRACTICE

Page 9: Introduction to object oriented language

DO THE LABS AND ASSIGNMENTS YOURSELF

Page 10: Introduction to object oriented language

BE PATIENT WITH THE MACHINE

Page 11: Introduction to object oriented language

IF YOU REALLY NEED THAT, DO IT QUIETLY ...

But not in Class. What you have to do is just inform me about your situation and I will not mark your absent.

Page 12: Introduction to object oriented language

Course Policy

Page 13: Introduction to object oriented language

THE 5-MINUTE RULE

Page 14: Introduction to object oriented language

Applicable rules and submission deadlines will be indicated with the assignments

No make-ups

ASSIGNMENTS

Page 15: Introduction to object oriented language

10-15 minutes duration Will cover reading assignments and material covered in the class

Unannounced

No make-ups

QUIZZES

Page 16: Introduction to object oriented language

Supervised Lab Sessions and Tutorials will be organized as and when required.

TUTORIALS/LAB SESSIONS

Page 17: Introduction to object oriented language

CLASS QUESTIONS I will ask questions very often

So be attentive

Page 18: Introduction to object oriented language

PROGRAMMING Actually, programming is not so easy,

because a real good program is not easily programmed. It needs the programmers’ lots of wisdom, lots of knowledge about programming and lots of experience.

It is like writing, to be a good writer needs lots of experience and lots of knowledge about the world.

Learning and practice are necessary.

Page 19: Introduction to object oriented language

UNSTRUCTURED PROGRAMMING Usually, people start

learning programming by writing small and simple programs consisting only of one main program. Here ``main program'' stands for a sequence of commands or statements which modify data which is global throughout the whole program.

Main Program

Data

Page 20: Introduction to object oriented language

DRAWBACKS This programming technique can only be

used in a very small program. For example, if the same statement

sequence is needed at different locations within the program, the sequence must be copied. If an error needed to be modified, every copy needs to be modified.

This has lead to the idea to extract these sequences(procedure), name them and offering a technique to call and return from these procedures.

Page 21: Introduction to object oriented language

PROCEDURAL PROGRAMMING

With procedural programming, you are able to combine sequences of calling statements into one single place.

A procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made .

Main Program Procedure

Page 22: Introduction to object oriented language

PROCEDURES Programs can now be written more

structured and error free. For example, if a procedure is correct,

every time it is used it produces correct results.

Consequently, in cases of errors you can narrow your search to those places which are not proven to be correct.

Page 23: Introduction to object oriented language

COMPILATION PROCESS & TRANSLATOR TYPES

Page 24: Introduction to object oriented language

PROCEDURE PROGRAM VIEW

Now a program can be viewed as a sequence of procedure calls.

The main program is responsible to pass data to the individual calls, the data is processed by the procedures and the resulting data is presented.

Thus, the flow of data can be illustrated as a hierarchical graph, a tree.

Page 25: Introduction to object oriented language

PROCEDURE PROGRAM VIEW

Main Program

Data

Procedure1

Procedure2

Procedure3

Page 26: Introduction to object oriented language

MODULAR PROGRAMMING

Modular programming is subdividing your program into separate subprograms such as functions and subroutines.

With modular programming, procedures of a common functionality are grouped together into separate modules.

A program therefore no longer consists of only one single part. It is now divided into several smaller parts which interact through procedure calls and which form the whole program.

Page 27: Introduction to object oriented language

Main Program(Also a module)

Data

Data Data1

Module2

+Data Data2

Module1

+Data Data1

Procedure1Procedure2

The main program coordinates calls to procedures in separate modules and hands over appropriate data as parameters.

Procedure3

Page 28: Introduction to object oriented language

MODULAR PROGRAMMING

Each module can have its own data. This allows each module to manage an internal state which is modified by calls to procedures of this module.

Each module has its own special functionalities that supports the implementation of the whole program.

Page 29: Introduction to object oriented language

STRUCTURAL PROGRAMMING

Also structured programming A subset of procedural programming

that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify.

Certain languages such as Ada, Pascal, and dBASE are designed with features that encourage or enforce a logical program

Page 30: Introduction to object oriented language

PROBLEMS WITH STRUCTURED APPROACH

Function

Global DataGlobal Data Global Data

Function Function Function

Page 31: Introduction to object oriented language

PROBLEMS WITH STRUCTURED APPROACH When the program grows larger and more

complex even the structural programming shows the strain.

You may have heard horror stories of project’s complexity, schedule slipping etc.

In procedural programming data is given second class status.

Inventory system-> new programmer->writes the function

It is same like you are sitting in your lobby and left there your personal documents.

Page 32: Introduction to object oriented language

OBJECT-ORIENTATION

It is a kind of thinking methodologyEverything in the world is an object;Any system is composed of objects (certainly a

system is also an object);The evolution and development of a system is

caused by the interactions among the objects inside or outside the system

Page 33: Introduction to object oriented language

WHAT IS OBJECT ORIENTED PROGRAMMING?

Build programming using software objects.

Means that we organize software as a collection of discrete (different) objects that incorporate both data structure and behavior.

In OOP, the software objects correspond closely to real objects involve in the application area.

Page 34: Introduction to object oriented language

EVERYTHING IN THE WORLD IS AN OBJECT

A flower, a tree, an animal A student, a professor A desk, a chair, a classroom, a building A university, a city, a country The world, the universe A subject such as CS, IS, Math, History,

Page 35: Introduction to object oriented language

ANY SYSTEM IS COMPOSED OF OBJECTS

A law system A cultural system An educational system An economic system An Information system A computer system

Page 36: Introduction to object oriented language

THE DEVELOPMENT OF A SYSTEM IS CAUSED BY THE INTERACTIONS

University is developed by the interactions among:studentsprofessorsstaffsofficers of Bio-scienceofficers of CIIT… ...

Page 37: Introduction to object oriented language

OBJECT-ORIENTED PROGRAMMING & ADT

ADTs allows the creation of instances with well-defined properties and behavior.

In object-orientation, ADTs are referred to as classes. Therefore, a class defines properties of objects which are

the instances in an object-oriented environment. Object-oriented programming is ``programming with

ADTs'': combining functionality of different ADTs to solve a problem.

Therefore instances (objects) of ADTs (classes) are dynamically created, destroyed and used.

Page 38: Introduction to object oriented language

Object1

Data1+Procedures1

Data Data1 Object3

Data3 + Procedures3

Object2

Data2 + Procedures2

Object4

Data4 + Procedures4

Page 39: Introduction to object oriented language

OBJECT-ORIENTED LANGUAGES

An object-based programming language is one which easily supports object-orientation.

Smalltalk:1972-1980, Alan Kay C++:1986, Bjarne Stroustrup Java:1992 (Smalltalk + C++), James

Gosling C#:

Developed at Microsoft by Anders Hejlsberg et al, 2000 Event driven, object oriented, visual programming

language (C++ and Java)

Page 40: Introduction to object oriented language

WHY OOP? ◆ Complexity & Haste:

Skilled programmer: 10-15 lines of code a day

Price ~ $18/line Can you pay this? If not, what will you

do?Hire non professionals?

◆ OOP fights Complexity:A more realistic model of the worldCollection of interacting objects instead of a

collection of functionsReuse of ready made components

Page 41: Introduction to object oriented language

REUSABILITY: Reduce development cost:

one component instead of many Promote reliability:

exercise component from many different aspects

no need to remember to correct the same error twice

Page 42: Introduction to object oriented language

REUSE, REUSE…………… INFO TECH RESEARCH FUNDING

The Clinton Administration has targeted 5technology areas for support from the NationalInstitute of Standards & Technology, and twofocus directly on information technology: anationwide health care information infrastructureand an effort to develop reusable softwarecomponents for large software systems.[Wall Street Journal, 26 April ‘94, Page B6]Reuse is tightly coupled with abstraction:

● Identify similarity between elements.● Create a component that represents the similar parts.● Rewrite elements to use the common part.

Page 43: Introduction to object oriented language

EXTENDIBILITY & MODIFIABILITY Adding new features responding to

changing operation environment can be a matter of introducing a few new objects.

Easy to make minor changes in the data representation/ the procedures used in an OO program. Changes within an object do not affect any other parts of the program.

Page 44: Introduction to object oriented language

FLEXIBILITY, MAINTAINABILITY & REUSEABILITY Flexibility

Very flexible in accommodating different situations because the interaction patterns among the object can be changed without modifying the object.

MaintainabilityObjects can be maintained separately.Reusability

Objects can be reused in the different programs.

Page 45: Introduction to object oriented language

DATA IS IMPORTANT Data is more important then functions,

methods, procedures etc but in procedural languages data is given the second class status. OOP also provides the mechanism to hide the data.

Example: Importance of data in inventory system,

banking system etc.You hire a new programmer and he

accidently changes the corrupts the data. Same like someone changes your personal data.

Page 46: Introduction to object oriented language

REASON’S FOR HIDING Reduces complexity much like the

detailed workings of an automobile are hidden from the average driver.

Principle of least privilege (need-to-know)

More Secure. Less likely that someone will alter or misuse something.

Easier to change the implementation without affecting other modules that use it.

Page 47: Introduction to object oriented language

DIFFERENT TERMINOLOGIES

Classes Objects Data encapsulation Inheritance Polymorphism

Page 48: Introduction to object oriented language

CHARACTERISTICS OF OO

Class A generic definition for a set of

similar objects. Provides the specifications for the

objects’ behaviors and attributes. An abstraction of a real world entity.

Page 49: Introduction to object oriented language

CHARACTERISTICS OF OO

The key concepts are: Object

Directly relate to the real world entities.

Can be a person, thing or concept.Like a “black box”, therefore all the implementation is hidden.

Has a State (attribute) Behavior (operation) Identity (unique name)

Page 50: Introduction to object oriented language

CHARACTERISTICS OF OO Object vs Class object is created from a class. object is considered as an instance of a

class. class is considered as a template from

which objects are instantiated can create an object or many objects

from a class.

Page 51: Introduction to object oriented language

CHARACTERISTICS OF OO

Diagram 1: Class Car

Car

DoorSeatTypeModel

DriveStopLockUnlock

Diagram 2: MyCar as an ObjectObject vs Class

Page 52: Introduction to object oriented language

CHARACTERISTICS OF OO

Messages Requests for the receiver objects to

carry out the indicated method or behavior and return the result of that action to the sender objects

Page 53: Introduction to object oriented language

CHARACTERISTICS OF OO

Encapsulation Process of hiding the

implementation details of an object. Access to manipulate the object

data is through its interface (operations/ functions).

Protects an object’s internal state from being corrupted by other programs.

Page 54: Introduction to object oriented language

CHARACTERISTICS OF OO

Encapsulation (cont) Program maintenance is easier and

less expensive because changes in the object data or implementation is only modified in one place

Allows objects to be viewed as black boxes.

Page 55: Introduction to object oriented language

CHARACTERISTICS OF OO

Inheritance Code reuse mechanism to build

new objects out of old ones. Defines a relationship among

classes where one class shares the structure and/or behavior on one or more classes.

Provides a more flexible and adaptable system and enables polymorphism.

Page 56: Introduction to object oriented language

CHARACTERISTICS OF OO

Polymorphism Means having many forms. Provides the ability to use a single

message to invoke many different kinds of behavior.

Same name with different meaning.

Page 57: Introduction to object oriented language

PROTECTION Protected levels in a class:

Public: Can be accessed both within and external to the object.

Private: Can only be used by the object itself.

The public section of a class is usually called the interface. It is what the programmers sees.

Normally, All data is private and only the bare essential functions are made public.

Page 58: Introduction to object oriented language

SUMMARY What is Object Oriented

Programming? Object-oriented programming is a

method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships

Page 59: Introduction to object oriented language

REFERENCES: OOP by Robert Lafore OOP by Deitel and Deitel Haibin Zhu, Ph. D. Associate Professor of

CS, Nipissing University Complete Reference