Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Week 2-3 (Jan.

23-Feb4, 2023)

UNIT 1 CONTENTS

Unit 1 1.1 INTRODUCTION TO OOP


History and Definition
1.2 UNIFIED MODELING LANGUAGE (UML)

Introduction to Object

 Objects Basic Concepts


 Classes Association

Oriented Programming 


Methods
OOP Basic Concepts
Aggregation
Composition

AND UML Encapsulation


Inheritance
Multiplicity
UML Diagrams
Abstraction
Polymorphism
ECE 106 – COMPUTER PROGRAMMING Prepared by: ENGR. JEANNE M. IMBUIDO
2 By: JMI

Computer Programming PROGRAMMING LANGUAGES

The history of computer  Programming languages allow programmers to develop


programming is a steady move away a software
from machine-oriented views of  There are three major families of languages namely:
programming towards concepts and – Machine languages
metaphors that more closely reflect
– Assembly languages
the way in which we ourselves see &
understand the world. – High-Level languages

3 By: JMI 4 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 1
Week 2-3 (Jan.23-Feb4, 2023)

3 Major Families of Programming Languages High-Level Languages

Machine Languages Assembly Languages High-Level Languages Procedural Languages Object-Oriented Languages (OOP)
 Comprised of 1s and 0s  It is a step towards easier  High-level languages  Early high-level languages are typically  A programming language model in which is
 The “native” language of programming. represent a giant leap called procedural languages. based upon the concept of objects which
a computer  Comprised of a set of towards easier  The program code is written as a contain data in the form of attributes and code
elemental commands programming. in the form of methods.
 Difficult to program – sequence of instructions.
one misplaced 1 or 0 will which are tied to a specific  The syntax of HL  The focus of OOP languages is not on
processor. languages is similar to
 User has to specify “what to do” and also
cause the program to “how to do” (step by step procedure). structure, but on modeling data.
fail.  Codes need to be English.
 These instructions are executed in the  In object-oriented programming, computer
 Coding example: translated to machine Example: programs are designed using the concept of
language before the sequential order. These instructions are
grossPay = basePay + overTimePay objects that interact with the real world.
1110100010101 computer processes it. written to solve specific problems.
 HL Languages is divided  Programmers code using “blueprints” of data
111010101110  Coding Example: into 2 groups:  Examples include C, COBOL, Pascal,
HTML, VBScript models called classes.
11110111 Divide 1001010, 1011010 - Procedural Languages  Examples of OOP languages include C++, Visual
- Object-Oriented Basic.NET, Java and Python
5 Programming (OOP) By: JMI 6 By: JMI

High-Level Languages High-Level Languages

Example:
Procedural Languages Object-Oriented Languages (OOP)

Above example is a Procedural approach for solving problem, because it creates different
methods for calculating areas of different geometries.

7 By: JMI 8 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 2
Week 2-3 (Jan.23-Feb4, 2023)

High-Level Languages High-Level Languages

Identify if Procedural or Object Oriented Language : Identify if Procedural or Object Oriented Language :

Although the code already


have class for different
geometry but still it is
procedural.

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 10 By: JMI

High-Level Languages Early Programming Languages

Identify if Procedural or Object Oriented Language :


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
Both are Object Oriented Language many of the features of object-
oriented languages today..

11 By: JMI 12 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 3
Week 2-3 (Jan.23-Feb4, 2023)

Modern Programming Languages

Object-Oriented
Programming (OOP)

13 By: JMI 14

Object oriented programming Object oriented programming

 OOP is mainly a program design philosophy


Pure “OO” Hybrid “OO”
 OOP uses a different set of programming languages than old
Languages Languages
procedural programming languages (C, Pascal, etc.).
 Everything in OOP is grouped as self sustainable "objects".
Eiffel, Actor, Emerald, Delphi/Object Pascal, C++,
Hence, you gain reusability by means of four main object-
JADE, Obix, Ruby, Java, C#, VB.NET, Pascal,
oriented programming concepts.
Python, Scala, Visual Basic, MATLAB,
 In OOP, programmers can create relationships between one
Smalltalk, Self Fortran, Perl, COBOL 2002,
object and another. For example, objects can inherit
characteristics from other objects. PHP, ABAP, Ada 95

15 By: JMI 16 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 4
Week 2-3 (Jan.23-Feb4, 2023)


Key idea in object-oriented :
The real world can be “accurately” described as a
OOP
Basic Terminologies
collection of objects that interact.

17 18

OBJECTS, CLASS and METHODS OBJECTS, CLASS and METHODS

OBJECTS OBJECTS
 Informally, an object represents an entity, either physical, conceptual, or  In a more formal definition, an
software.
object is a computational entity
that:
Physical Entity Conceptual Entity Software Entity 1. Encapsulates some state
2. Is able to perform actions,
or methods, on this state
3. Communicates with other
objects via message passing

19 By: JMI 20 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 5
Week 2-3 (Jan.23-Feb4, 2023)

OBJECTS, CLASS and METHODS OBJECTS, CLASS and METHODS

OBJECTS OBJECTS
 Characteristics of Objects:  Real World Object:  Almost everything in the world can be represented as an object :
1. decomposition of programs into a number • A flower, a tree, an animal
of entities
• A student, a professor
2. consist of data and function
• A desk, a chair, a classroom, a building
3. data of an object can be accessed only by
the functions associated with that object • A university, a city, a country
 An object consists of data and behavior. • The world, the universe
The data and behavior comprise an • A subject such as Calculus, Advance Math, History, …
interface, which specifies how the object
may be utilized by any of various • An information system, financial, legal, etc..
consumers of the object.
21 By: JMI 22 By: JMI

OBJECTS, CLASS and METHODS OBJECTS, CLASS and METHODS

CLASS METHODS
 A class is a prototype, idea, and  A method in object-oriented programming (OOP) is a procedure associated with a
blueprint for creating objects message and an object.
 Class is composed of three things:  Many kinds of methods exist, but support for them varies across languages. Some types
its name, attributes/properties, and of methods are created and called by programmer code, while other special methods—
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
 An object is an instance of a class. In these special methods.
Java or Python, we define classes,
 Three types of method:
which in turn are used to create
1. Accessor methods - used to read data values of an object
objects.
2.Mutator methods - used to modify the data of an object
 A class is a definition of objects with
the same properties and the same 3.Manager methods - used to initialize and destroy objects of a class,
methods. e.g. constructors and destructors
23 By: JMI 24 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 6
Week 2-3 (Jan.23-Feb4, 2023)

OBJECTS, CLASS and METHODS OBJECTS, CLASS and METHODS

Classes and Objects 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

25 By: JMI 26 By: JMI

ENCAPSULATION
 Hides the details of the implementation of an
objects.

Four Main concepts of OOP


 objects encapsulate their state and hide it
from the outside.
Encapsulation  outside users of the class interact with it
Abstraction through its methods, but cannot access the
Inheritance classes state directly.
Polymorphism  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.
27 28 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 7
Week 2-3 (Jan.23-Feb4, 2023)

ENCAPSULATION – BENEFITS and EXAMPLE ENCAPSULATION – in PYTHON

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 We defined a Computer class.
We used __init__() method to store the maximum
 Hiding implementation details reduces complexity selling price of Computer.
We tried to modify the price. However, we can't
- easier maintenance change it because Python treats the __maxprice as
private attributes.
EXAMPLE: As shown, to change the value, we have to use a
setter function i.e setMaxPrice() which takes price
as a parameter.

User and a Mobile phone - “User don't need to know the internal functionality of the Therefore,
mobile phone to operate it, it has an interface to use the device behavior without knowing
Encapsulation prevents data from direct
implementation details. (other example: calculator, ATM machine etc.)
modification;

29 By: JMI 30 By: JMI

ABSTRACTION 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:
31 By: JMI 32 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 8
Week 2-3 (Jan.23-Feb4, 2023)

ABSTRACTION – in PYTHON INHERITANCE

 It is a way of organizing classes


 Classes with properties in common can
be grouped so that their common
properties are only defined once in
How Abstract Base classes work : parent class.
By default, Python does not provide  Inheritance enables new classes to
abstract classes. Python comes with a receive—or inherit—the properties and
module that provides the base for defining methods of existing classes.
Abstract Base classes(ABC) and that
module name is ABC. ABC works by • Classes may be arranged in a class hierarchy
decorating methods of the base class as where one class is derived from an existing class
abstract and then registering concrete (super-class)
classes as implementations of the abstract • A sub-class inherits the attributes and operations
base. A method becomes abstract when from its super class and may add new methods or
decorated with the keyword attributes of its own.
@abstractmethod.
33 By: JMI 34 By: JMI

INHERITANCE – BENEFITS and EXAMPLE INHERITANCE – in PYTHON

Therefore,
Inheritance is a way of creating a
BENEFITS: new class for using details of an
existing class without modifying it.
The newly formed class is a derived
 Expresses commonality class (or child class). Similarly, the
among classes/objects existing class is a base class (or
parent class).
 Allows code reusability
 Highlights relationships 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
 Helps in code the swim() method.
organization 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.

35 By: JMI 36 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 9
Week 2-3 (Jan.23-Feb4, 2023)

POLYMORPHISM 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.

37 By: JMI 38 By: JMI

POLYMORPHISM – in Python 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.
Note: To use polymorphism, we created a
A Deer IS-A Animal common interface i.e flying_test()
the Deer class is
A Deer IS-A Vegetarian considered to be function that takes any object and calls
A Deer IS-A Deer polymorphic since this the object's fly() method. Thus, when we
has multiple inheritance passed the blu and peggy objects in the
flying_test() function, it ran effectively.
If we apply the reference variable facts to a Deer object
Therefore,
reference, the following declarations are legal :
Polymorphism is an ability (in OOP) to use a common interface for
All the reference variables multiple forms (data types).
d, a, v, o refer to the same Deer Suppose, we need to color a shape, there are multiple shape options
object (rectangle, square, circle). However we could use the same method to
color any shape. This concept is called Polymorphism.

39 By: JMI 40 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 10
Week 2-3 (Jan.23-Feb4, 2023)

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

UNIFIED MODELING
If a programmer has preventing loss of data. will not be converted to
already written a power machine code. This mark
up can be as long as the
LANGUAGE (UML)
function, then it should
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.
41 42

UML Definition and Usage UML CLASS DIAGRAM

 UML stands for:  A model helps getting an What is a Class Diagram?


Unified Modeling Language abstract idea of the objects the  A Class Diagram is a type of UML diagram describing the structure of a system.
 UML helps in facilitating:
software product consists of  It is the main building block of Object Oriented Programming, use in general
and their interactions – Employ Concept Modeling, Detailed Modeling and Programming Code
– Specification information hiding in the model  Show the object classes in the system and the relationships between these
– Visualization  Model’s point of view is classes
– Architecture Design important to determine the  It consists the :
final shape of the diagram and • Classes
– Construction
the necessary information to • Attributes
– Simulation and Testing be included
• Operations (or methods),
– Documentation
• Relationships among the classes.
43 By: JMI 44 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 11
Week 2-3 (Jan.23-Feb4, 2023)

CLASSES in UML CLASSES in UML

How Do We Represent the Classes in the class diagram?


What is a ClassES?
 A class is a description of a set of
objects that share the same
attributes, operations, relationships,
and semantics.
 Graphically, a class is rendered as a
rectangle, usually including its Class
name (topmost), attributes (Middle),
and operations/methods (Bottom) in
separate, designated compartments.

45 By: JMI 46 By: JMI

CLASSES (Name) CLASSES (attributes)

 A class attribute is a named property of


 The class name is the only required tag a class that describes the object being
in the graphical representation of a modeled. In the class diagram,
class. It always appears in the top-most attributes appear in the second
compartment. compartment just below the name-
compartment.

47 By: JMI 48 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 12
Week 2-3 (Jan.23-Feb4, 2023)

CLASSES (attributes) CLASSES (attributes)

 Attributes are usually listed in the form:  Attributes can be:


attributeName : Type + indicates public attributes/methods
# indicates protected attributes/methods
 A derived attribute is one that can be - indicates private attributes/methods
computed from other attributes, but / indicates derived attributes/methods
doesn’t actually exist.
• Public : visible to all elements that can access the
For example, a Person’s age can be contents of the namespace that owns it
computed from his birth date. A derived • Private: only visible inside the namespace that owns it
attribute is designated by a preceding ‘/’ as • Protected: visible to elements that have a generalization
in: relationship to the namespace that owns it
/ age : Date
49 By: JMI 50 By: JMI

CLASSES (Operation) 3 Kinds of Relationships in UML

 In UML, object interconnections (logical or physical), are


modeled as relationships.
 Operations
- describe the class behavior  There are three kinds of relationships in UML:
and appear in the third compartment.
• Dependencies
• Generalizations
• Associations

51 By: JMI 52 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 13
Week 2-3 (Jan.23-Feb4, 2023)

CLASSES CLASSES

Dependency Relationships Generalization Relationships


 Dependency indicates a semantic relationship between two or more  A generalization connects a subclass to
elements. The dependency from CourseSchedule to Course exists its superclass. It denotes an inheritance
because Course is used in both the add and remove operations of of attributes and behavior from the
CourseSchedule. superclass to the subclass and indicates
a specialization in the subclass of the
more general superclass.

53 By: JMI 54 By: JMI

CLASSES CLASSES

Generalization Relationships Association Relationships


 UML permits a class to inherit from multiple superclasses, although some  If two classes in a model need to communicate with each other, there
programming languages (e.g., Java) do not permit multiple inheritance. must be link between them.
 An association denotes that link.

55 By: JMI 56 By: JMI

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 14
Week 2-3 (Jan.23-Feb4, 2023)

CLASSES ENUMERATION

Association Relationships  An enumeration is a user-defined data type that consists of a name and an
 We can indicate the multiplicity of an association by adding multiplicity ordered list of enumeration literals.
adornments to the line denoting the association.

57 By: JMI 58 By: JMI

Thanks!

any
questions
?
You can directly message me at my FB Account:
jeanne macapagal imbuido
59

ECE106 - Computer Programming


Prepared by: Engr. JEANNE M. IMBUIDO 15

You might also like