Week 3 (Unit 1 Part 1)
Unit 1
Introduction to Object
Oriented Programming
AND UML
ECE 106 – COMPUTER PROGRAMMING Prepared by: ENGR. JEANNE M. IMBUIDO
UNIT 1 CONTENTS
1.1 INTRODUCTION TO OOP 1.2 UNIFIED MODELING LANGUAGE (UML)
History and Definition
Objects Basic Concepts
Classes Association
Methods Aggregation
OOP Basic Concepts Composition
Encapsulation Multiplicity
Inheritance UML Diagrams
Abstraction
Polymorphism
2 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 1
Week 3 (Unit 1 Part 1)
Computer Programming
The history of computer
programming is a steady move away
from machine-oriented views of
programming towards concepts and
metaphors that more closely reflect
the way in which we ourselves see &
understand the world.
3 By: JMI
PROGRAMMING LANGUAGES
Programming languages allow programmers to develop
a software
There are three major families of languages namely:
– Machine languages
– Assembly languages
– High-Level languages
4 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 2
Week 3 (Unit 1 Part 1)
3 Major Families of Programming Languages
Machine Languages Assembly Languages High-Level Languages
Comprised of 1s and 0s It is a step towards easier High-level languages
The “native” language of programming. represent a giant leap
a computer Comprised of a set of towards easier
elemental commands programming.
Difficult to program –
one misplaced 1 or 0 will which are tied to a specific The syntax of HL
cause the program to processor. languages is similar to
fail. Codes need to be English.
Coding example: translated to machine Example:
language before the grossPay = basePay + overTimePay
1110100010101 computer processes it.
HL Languages is divided
111010101110 Coding Example: into 2 groups:
11110111 Divide 1001010, 1011010 - Procedural Languages
- Object-Oriented
5 Programming (OOP) By: JMI
High-Level Languages
Procedural Languages Object-Oriented Languages (OOP)
Early high-level languages are typically A programming language model in which is
called procedural languages. based upon the concept of objects which
The program code is written as a contain data in the form of attributes and code
sequence of instructions. in the form of methods.
User has to specify “what to do” and also The focus of OOP languages is not on
“how to do” (step by step procedure). structure, but on modeling data.
These instructions are executed in the In object-oriented programming, computer
sequential order. These instructions are programs are designed using the concept of
written to solve specific problems. objects that interact with the real world.
Examples include C, COBOL, Pascal, Programmers code using “blueprints” of data
HTML, VBScript models called classes.
Examples of OOP languages include C++, Visual
[Link], Java and Python
6 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 3
Week 3 (Unit 1 Part 1)
High-Level Languages
Procedural Languages Object-Oriented Languages (OOP)
7 By: JMI
High-Level Languages
Example:
Above example is a Procedural approach for solving problem, because it creates different
methods for calculating areas of different geometries.
8 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 4
Week 3 (Unit 1 Part 1)
High-Level Languages
Identify if Procedural or Object Oriented Language :
Above example will remain procedural, even if it uses an object. It still have sequence of
instructions, and functions for accomplishing / solving the problem.
9 By: JMI
High-Level Languages
Identify if Procedural or Object Oriented Language :
Although the code already
have class for different
geometry but still it is
procedural.
10 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 5
Week 3 (Unit 1 Part 1)
High-Level Languages
Identify if Procedural or Object Oriented Language :
Procedural OOP
11 By: JMI
High-Level Languages
Identify if Procedural or Object Oriented Language :
Both are Object Oriented Language
12 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 6
Week 3 (Unit 1 Part 1)
Early Programming Languages
SIMULA
Simula is a name for two simulation
programming languages, Simula I and
Simula 67, developed in the 1960s at
the Norwegian Computing Center in
Oslo, by Ole-Johan Dahl and Kristen
Nygaard.
Simula is considered the first object-
oriented programming language.
Simula was designed for doing
simulations, and the needs of that
domain provided the framework for
many of the features of object-
oriented languages today..
13 By: JMI
Modern Programming Languages
14 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 7
Week 3 (Unit 1 Part 1)
Object-Oriented
Programming (OOP)
15
Object oriented programming
OOP is mainly a program design philosophy
OOP uses a different set of programming languages than old
procedural programming languages (C, Pascal, etc.).
Everything in OOP is grouped as self sustainable "objects".
Hence, you gain reusability by means of four main object-
oriented programming concepts.
In OOP, programmers can create relationships between one
object and another. For example, objects can inherit
characteristics from other objects.
16 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 8
Week 3 (Unit 1 Part 1)
Object oriented programming
Pure “OO” Hybrid “OO”
Languages Languages
Eiffel, Actor, Emerald, Delphi/Object Pascal, C++,
JADE, Obix, Ruby, Java, C#, [Link], Pascal,
Python, Scala, Visual Basic, MATLAB,
Smalltalk, Self Fortran, Perl, COBOL 2002,
PHP, ABAP, Ada 95
17 By: JMI
“
Key idea in object-oriented :
The real world can be “accurately” described as a
collection of objects that interact.
18
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 9
Week 3 (Unit 1 Part 1)
OOP
Basic Terminologies
19
OBJECTS, CLASS and METHODS
OBJECTS
Informally, an object represents an entity, either physical, conceptual, or
software.
Physical Entity Conceptual Entity Software Entity
20 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 10
Week 3 (Unit 1 Part 1)
OBJECTS, CLASS and METHODS
OBJECTS
In a more formal definition, an
object is a computational entity
that:
1. Encapsulates some state
2. Is able to perform actions,
or methods, on this state
3. Communicates with other
objects via message passing
21 By: JMI
OBJECTS, CLASS and METHODS
OBJECTS
Characteristics of Objects: Real World Object:
1. decomposition of programs into a number
of entities
2. consist of data and function
3. data of an object can be accessed only by
the functions associated with that object
An object also consists of data and
behavior. The data and behavior
comprise an interface, which specifies
how the object may be utilized by any of
various consumers of the object.
22 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 11
Week 3 (Unit 1 Part 1)
OBJECTS, CLASS and METHODS
OBJECTS
Almost everything in the world can be represented as 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 Calculus, Advance Math, History, …
• An information system, financial, legal, etc..
23 By: JMI
OBJECTS, CLASS and METHODS
CLASS
A class is a prototype, idea, and
blueprint for creating objects
Class is composed of three things:
its name, attributes/properties, and
methods.
An object is an instance of a class. In
Java or Python, we define classes,
which in turn are used to create
objects.
A class is a definition of objects with
the same properties and the same
methods.
24 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 12
Week 3 (Unit 1 Part 1)
OBJECTS, CLASS and METHODS
METHODS
A method in object-oriented programming (OOP) is a procedure associated with a
message and an object.
Many kinds of methods exist, but support for them varies across languages. Some types
of methods are created and called by programmer code, while other special methods—
such as constructors, destructors, and conversion operators—are created and called by
compiler-generated code. A language may also allow the programmer to define and call
these special methods.
Three types of method:
1. Accessor methods - used to read data values of an object
[Link] methods - used to modify the data of an object
[Link] methods - used to initialize and destroy objects of a class,
e.g. constructors and destructors
25 By: JMI
OBJECTS, CLASS and METHODS
Classes and Objects
An Object is a Class when it
comes alive!
Homo Sapiens is a class, John
and Jack are objects
Animal is a class, “Snowball” the
cat is an object
Vehicle is a class, My neighbor's
BMW is an object
Galaxy is a class, the Milky Way
is an object
26 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 13
Week 3 (Unit 1 Part 1)
OBJECTS, CLASS and METHODS
Classes and Objects
27 By: JMI
Four Main concepts of OOP
Encapsulation
Abstraction
Inheritance
Polymorphism
28
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 14
Week 3 (Unit 1 Part 1)
ENCAPSULATION
Hides the details of the implementation of an
objects.
objects encapsulate their state and hide it
from the outside.
outside users of the class interact with it
through its methods, but cannot access the
classes state directly.
The class is kind of a container or capsule or a
cell, which encapsulate the set of methods,
attributes and properties.
The idea of encapsulation is to hide how a
class does its operations while allowing
requesting its operations.
29 By: JMI
ENCAPSULATION – BENEFITS and EXAMPLE
BENEFITS:
Ensures that structural changes remain local:
- Changing the class internals does not affect any code outside of the class
- Changing methods' implementation does not reflect the clients using them
Hiding implementation details reduces complexity
- easier maintenance
EXAMPLE:
User and a Mobile phone - “User don't need to know the internal functionality of the
mobile phone to operate it, it has an interface to use the device behavior without knowing
implementation details. (other example: calculator, ATM machine etc.)
30 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 15
Week 3 (Unit 1 Part 1)
ENCAPSULATION – in PYTHON
We defined a Computer class.
We used __init__() method to store the maximum
selling price of Computer.
We tried to modify the price. However, we can't
change it because Python treats the __maxprice as
private attributes.
As shown, to change the value, we have to use a
setter function i.e setMaxPrice() which takes price
as a parameter.
Therefore,
Encapsulation prevents data from direct
modification;
31 By: JMI
ABSTRACTION
Abstraction is a design principle.
Is the process of removing
characteristics from something in order
to reduce it to a set of essential
characteristics.
Through the process of abstraction, a
programmer hides all but the relevant
data about a class in order to reduce
complexity and increase reusability.
Abstraction is a basic representation of a
concept.
Abstraction refers to the act of representing essential features
Abstraction allows programmers to without including the background details.
represent complex real world in the
Hiding unnecessary data from the users and making the
simplest manner. application as user friendly then it is called Abstraction.
Ex:
32 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 16
Week 3 (Unit 1 Part 1)
ABSTRACTION
33 By: JMI
ABSTRACTION – in PYTHON
How Abstract Base classes work :
By default, Python does not provide
abstract classes. Python comes with a
module that provides the base for defining
Abstract Base classes(ABC) and that
module name is ABC. ABC works by
decorating methods of the base class as
abstract and then registering concrete
classes as implementations of the abstract
base. A method becomes abstract when
decorated with the keyword
@abstractmethod.
34 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 17
Week 3 (Unit 1 Part 1)
INHERITANCE
It is a way of organizing classes
Inheritance enables new
classes to receive—or inherit—
the properties and methods of
existing classes.
Classes with properties in
common can be grouped so
that their common properties
are only defined once in
parent class.
35 By: JMI
INHERITANCE – BENEFITS and EXAMPLE
BENEFITS:
Expresses commonality among classes/objects
Allows code reusability
Highlights relationships
Helps in code organization
36 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 18
Week 3 (Unit 1 Part 1)
INHERITANCE – BENEFITS and EXAMPLE
• Classes may be arranged in
a class hierarchy where one
class is derived from an
existing class (super-class)
• A sub-class inherits the
attributes and operations
from its super class and may
add new methods or
attributes of its own.
37 By: JMI
INHERITANCE – in PYTHON
Therefore,
Inheritance is a way of creating a
new class for using details of an
existing class without modifying it.
The newly formed class is a derived
class (or child class). Similarly, the
existing class is a base class (or
parent class).
we created two classes i.e. Bird (parent class) and Penguin (child class).
The child class inherits the functions of parent class. We can see this from
the swim() method.
Again, the child class modified the behavior of the parent class. We can
see this from the whoisThis() method. Furthermore, we extend the
functions of the parent class, by creating a new run() method.
Additionally, we use the super() function inside the __init__() method.
This allows us to run the __init__() method of the parent class inside the
child class.
38 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 19
Week 3 (Unit 1 Part 1)
POLYMORPHISM
Polymorphisms means the ability to request
that the same methods be performed by a wide
range of different types of things.
In OOP, polymorphisms is a technical issue and
principle. It is achieved by using many different
techniques named method overloading,
operator overloading, and method overriding.
In here, an object has “multiple identities”,
based on its class inheritance tree which can be
used in different ways
The ability to define different functions or
classes as having the same name but taking
different data types.
39 By: JMI
POLYMORPHISM
40 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 20
Week 3 (Unit 1 Part 1)
POLYMORPHISM – in Python
Note:
A Deer IS-A Animal
the Deer class is
A Deer IS-A Vegetarian considered to be
A Deer IS-A Deer polymorphic since this
has multiple inheritance
If we apply the reference variable facts to a Deer object
reference, the following declarations are legal :
All the reference variables
d, a, v, o refer to the same Deer
object
41 By: JMI
POLYMORPHISM – in PYTHON
In the above program, we defined two
classes Parrot and Penguin. Each of them
have a common fly() method. However,
their functions are different.
To use polymorphism, we created a
common interface i.e flying_test()
function that takes any object and calls
the object's fly() method. Thus, when we
passed the blu and peggy objects in the
flying_test() function, it ran effectively.
Therefore,
Polymorphism is an ability (in OOP) to use a common interface for
multiple forms (data types).
Suppose, we need to color a shape, there are multiple shape options
(rectangle, square, circle). However we could use the same method to
color any shape. This concept is called Polymorphism.
42 By: JMI
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 21
Week 3 (Unit 1 Part 1)
PROGRAMMING CONCEPTS
Re-usability Privacy Documentation
The ability to reuse code This is important for The commenting of a
for multiple applications. large programs and program in mark up that
If a programmer has preventing loss of data. will not be converted to
already written a power machine code. This mark
function, then it should up can be as long as the
be written that any programmer wants, and
program can make a call allows for comprehensive
to that function and it information to be passed
should work exactly the on to new programmers.
same. This is important for both
the re-usability and the
maintainability of
programs.
43
ECE106 - Computer Programming
Prepared by: Engr. JEANNE M. IMBUIDO 22