Lecture 6 Introduction to Object Oriented Programming
Lecture 6 Introduction to Object Oriented Programming
Lecture 6
Introduction to Object Oriented Programming
Outline
• Towards Structured Programming
• Procedural Languages
• Problem With Procedural Languages
• Object Oriented Programming
• Example of Real-World Objects
• Characteristics of Object-Oriented Programming
• Objects & Classes
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Towards Structured Programming
• Early Versions of Pascal , Fortran , BASIC and COBOL
• Used GOTO statements which made the program un-structured
• Later versions included control structures like Loops - Structured Programming
• Procedural Languages ( C , Later Versions of Pascal , Fortran , Basic)
• Structuring the program by decomposing tasks into methods
• Dividing program in to modules / functions
• Programs broken into smaller units = Functions
• Each Function with a clearly defined purpose and interface to other Functions in the
program
• Programs become larger difficulty in comprehending
3
Towards Structured Programming
• Problems with Procedural Languages
• Functions had unrestricted access to global data
• Large programs have many functions, many global data items and many connections
between functions and data
• Difficulty in conceptualization
• Difficult to modify
4
Towards Structured Programming
5
Towards Structured Programming
Data Object 1
Member
Functions
Data
Data
Member
Object 2 Member Functions
Object 3
Functions
8
Objects - Real World Example
9
Objects in the Real World
Objects – Real World Examples
Command Button
Real World Objects
Traffic Light
Object Oriented Approach
• Objects
• Combine both DATA and FUNCTIONS into a single unit – OBJECT
• Member Data
• Each Object has its own copy of member data
• Member Function
• – object’s function – provide the ONLY way to access member data of the object
• Can not access data directly – Data is hidden – safe from accidental
alterations
• Different objects interact with each other by calling one another’s member
functions
13
Objects Interaction
Characteristics of Object Oriented
Language
• Objects
• While solving a problem, instead of asking “how to divide problem in
functions?” we ask, “how the problem will be divided into objects?”
• Helps in easier design of the solution
• Examples of Real-World Objects
15
Characteristics of Object Oriented
Language
• Classes
• A class is a blueprint ( template) for a set of objects
• It is an abstract description of similar objects
• Specifies what DATA and what FUNCTIONS will be included in the objects of the class
• A class basically defines similar objects in terms of state and behavior
16
Characteristics of Object Oriented
Language
• Class
• A BankAccount Class
Characteristics of Object Oriented
Language
• Abstraction
• Abstraction is one of the key concepts of OOP
• Its main goal is to handle complexity by hiding the un-necessary implementation details
• Abstraction Example
• When send Text Message you just type and send – do not care about how its actually transmitted
• When You drive a car , you only care about top level details like gears, steering, speed , brakes etc
• You don’t need to care about how the engine, transmission and braking system works
• When you use a coffee machine to make coffee
• You just need to provide water , coffee beans and select the type of coffee
• You don’t need to care about how the coffee machine works
• Someone else worried about that and created a coffee machine which provides abstraction to low level details
• Abstraction is achieved through hierarchical Classifications
• This allows you to breakdown a complex system into smaller manageable pieces
• From the outside car is a single object
• But , internally it consists of subsystems like Steering , Brakes , Sound System , Fuelling Systems etc
• Abstraction is achieved through Abstract Classes and Abstract Methods
Characteristics of Object-Oriented
Languages
• Encapsulation
• Encapsulation means binding data and functions that manipulate that data into one
unit called a class
• The purpose is to keep safe – both data and functions from outside world
• It is also called Information Hiding
19
Characteristics of Object Oriented
Languages
• Inheritance
• It is a mechanism of deriving a new class from an old class that is pre-defined
• The old class is called the base or super class
• The new class is called the derived class
• Subclass shares common characteristics with the class from which it is derived.
• Each subclass also has its own particular characteristics.
• For Example
• Animals Herbivores Giraffe
• Animal Herbivores Mammals Cow
• Animals Omnivores Mammals Bear
20
Inheritance –Example I
Inheritance – Example II
Inheritance – Example III
Generalization
Specialization
Characteristics of Object Oriented
Languages
• Reusability
• Once a class properly written, it can be distributed for others to use
• Also, the Inheritance provides the extension to reusability – take existing class
and without modification add new features to it
24
Characteristics of Object Oriented
Languages
• New Data Types
• Objects help in creating new Data Types
• Example – A point in co – ordinate System
• Time
• Date
• Distance
25
Characteristics of Object-Oriented
Languages
• Polymorphism
• Using Operators or Functions in different ways depending on what they are operating
on is called polymorphism
• We can use the same methods for different implementations based on the arguments
26