Title: Introduction to OOP
1Introduction to OOP
- Fundamental OOP Concepts
- Advantages of OOP
- Data Types
- Classes
- Objects
- Encapsulation
- Message Passing
- Inheritance
- Polymorphism
2Fundamental OOP Concepts
- World is Full of Things or Objects
- Everything is an object - specific
characteristics, attributes, behavior - People, places, things i.e. a Car
- Objects are the Things in OOP
- Objects have parts called Members
- Attributes (Data) - seats, doors, engine
- Methods (Functions) - start, accelerate
- Objects belong to a Class
- A variable of a given Class type My Ferrari
- Objects are distinct from each other
- Different variables - My Ferrari vs. Bobs
Ferrari
3Fundamental OOP Concepts
- Objects Fall into Classes
- Based on overall characteristics and behavior
- Define many different Objects that are similar
- Identify the component parts of Objects
- Distinguish the differences between Objects
4Advantages of OOP
- Increased Productivity
- Machine Code-Assembler-Procedural-Object
Oriented - Greater Abstraction, less dependent on machine
- Enhanced Reliability
- self-contained objects
- easier to debug
- Improved Maintenance
- localization of responsibility
- reduced global data
- Reuseable Code
- Clearly defined interfaces
- Extendable through Inheritance
5Programming Methodologies
Procedural Programming
Object-Oriented Programming
Functional Abstraction Structured Programming
Support Global Data Structures Separate
Functions Function parameters Main Program uses
Sub-functions Tight data coupling Functional
Thinking - What functions do I need to
solve the problem?
Object (Type) Abstraction Structured Programming
Support Objects have Data Structures Objects
have Functions (Methods) Objects called with
messages Objects use helper methods Loose data
coupling Object Thinking - What objects are
in the problem domain?
6Object-Oriented Activities
- Object-Oriented Analysis - OOA
- Identify objects in the real world problem
domain - Specify responsibilities and interactions
- Organize into Classes
- Object-Oriented Design - OOD
- Specify messages and expected behavior
- Objects used as building blocks
- Object-Oriented Programming - OOP
- Classes built per design
- Objects created to handle Message Transactions
- Test Classes and Objects individually
- Main Program defines Objects and Hooks them
Together
7What is a Data Type?
- Fundamental programming elements
- Built-in types int, char, float, etc
- Have specific form
- Memory size, organization
- Have specific behavior
- Valid operations - / , etc
- Different behavior depending upon the type
- User specified collections of Data Types
- Structures, Arrays, Records
8Classes
- User Defined Data Types
- Defines Basic Characteristics of an Object
- Allow New Programming Elements
- Extends language
- Have Attributes and Methods
- Contain Data for the Object state
- Provide functions unique to the kind of Object
- Abstract Data Types (ADT) - A Generalized Class
- Defines Basic Features of a Class
- Cannot create Objects of an ADT Class
- ADT is a partial class used to create a
Complete Class - i.e. Vehicle ADT must be made into a Car,
Truck, Motorcycle
9Encapsulation
- Information hiding
- Data values are private
- Some functions may be private
- Internal details not visible outside
- Data representation is hidden
- Like a Black-Box
- An object has a specified set of functions that
it performs - Each function may alter/control internal data of
object - User of object doesnt care how it works
- Keeps information consistent
- External functions cant directly alter internal
state - Object has ultimate control over what happens
10Objects
- Class Variables are Objects
- Object is a part of of the program
- Class declares the properties of an object
- OOP - Object is an instance of a class
- Multiple objects can be made from each class
- Objects can be contained within other objects
- or, within data structures
- Objects usually have both Data and Functions
- Perform action on the objects data
- Objects Data State
- Persists with objects lifespan
- Available to all methods of the object
11Class and Object
Object Usage //Create an Object Circle
MyCircle // Call Methods MyCircle.Locate (40,
25) MyCircle.Size (16.5) MyCircle.Draw
() MyCircle.Erase ()
A Circle Class
12Message Passing
- Communication between objects
- Objects receive and respond to messages
- Objects may send messages to other objects
- Information (Data) flow
- Messages usually contain information parameters
- Objects perform operation on its own data
- Objects return information as needed
- Defined interface to outside world
- Messages are how we tell an object to do
something - Method (Function) invocation
- What an object knows or does is important
- How an object works is hidden (black-box)
- Circle object example
13A Circle Drawing Class
Circle ------------------------- int X, Y float
radius ------------------------- Draw Erase Locate
(int x, int y) Size (float radius) Move (int x,
int y)
Name of Class Attributes Messages (aka Methods
or Functions)
14Inheritance
- Real world
- Natural process
- Parents - Children
- Same kind of a thing, but different
- Is-a Relationship vs. Has-a Relationship
- OOP
- Generalized class into a specialized class
- Families of Classes
- Code sharing
- Alter behavior and/or information
- Single and multiple inheritance
15Inherited Class
Circle ------------------------- int X, Y float
radius ------------------------- Draw Erase Locate
(int x, int y) Size (float radius) Move (int x,
int y)
ColorCircle Circle --------------------------- i
nt color --------------------------- setColor
(int color) Draw () Erase ()
Inherited from Circle Added Attribute Added /
modified messages
Inheritance
16Polymorphism
- Many Forms
- Meaning of Method is consistent
- Easier to use, reduces the name game
- Families of functions
- Overloading
- Different types, same operations
- Different functions, same name
- Object Polymorphism
- Different Objects, same message
- Dynamic behavior
- Run-time binding
Creatures
17Summary
- Objects exist in real world
- Humans naturally tend to categorize objects into
classes - OOP is based on user defined classes and objects
- Objects are variables of a class type
- Encapsulation promotes safe programming
- Messages passed for behavior and information
transfer - Inheritance allows classes to be based on other
classes - Polymorphism allows different object types to
respond to the same message