What Is Object Oriented Programming?: Ch-1 OOP in Python Updated & Revised by Dr. Ra'ed M. Al-Khatib (2019)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

What is Object Oriented

Programming?

Ch1: CS210 OOP-Python

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 1
Definition
• OOP is a design philosophy.
• Object-Oriented Programming (OOP) uses
a different set of programming languages
than old procedural programming
languages
• Everything in OOP is grouped as self
sustainable "objects"

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 2
Object-Oriented Programming
Main Concepts
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 3
What is an Object?
• An Object can be considered a "thing" that
can perform a set of related activities.
• The set of activities that the object
performs defines the object's behavior.
• For examples
– The Hand (object) can grip something, 
– or a Student (object) can give their name or
address.
• In pure OOP terms an object is an instance
of a class.
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 4
What is a Class?
• A class is simply a representation of a type of object.
• The class is the blueprint, or plan, or template, that describes the
details of an object.
• The Class: is composed of three things: a name, attributes, and
operations.
General class
Example:
class Student: class name  Student
id = 20189012345 id : int
Attributes name : string
name = 'Fadi' or
phone = 077112233 phone : int
properties
email = [email protected] email : string
or
address : string
address = 'Irbid – Jordan' data
major : string
major = 'cs' members

operations + SetphoneNumber( … ): int


+ Setemail ( … ): void
+ display (…) : void
a gram
i
Ld
s UM
s
Cla

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 5
anaconda 3 download

https://www.anaconda.com/distribution/

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 6
Example of a class
• For example, you as a student is created out of class type Student.
• Questions?
– What are the different attributes of a student?
– What is the type of each attribute?
– What are the different behaviors of a student?
– Provide other examples of classes in real world?
– Identify their characteristics? class Student:
General name = ' add std name '
class id = 0
#------ ------
class name  Student
# main code
id = 20189012345 s1 = Student()
name = 'Fadi'
Attributes phone = 077112233
or s1.name = 'Wael Emad'
email = [email protected]
properties
address = 'Irbid – Jordan'
s1.id = 20189012345
major = 'cs' print(s1.name)
print(s1.id)
operations + SetphoneNumber( … ): int
+ Setemail ( … ): void print(Student.name)
+ display (…) : void print(Student.id)
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 7
What is a Class? General class
Example:
class Student: name  Student
id = 20189012345 id = 20189012345
name = 'Fadi' name = 'Fadi'
phone = 077112233
attributes phone = 077112233
email = [email protected]
email = [email protected]
address = 'Irbid – Jordan'
address = 'Irbid – Jordan' major = 'cs'
major = 'cs'
Student + SetphoneNumber( … ): int
operations + Setemail ( … ): void
id = 20189012345
+ display (…) : void gr am
name = 'Fadi' ia
… Ld
------------------------- s UM
s
+ Setphone… Cla

subclass subclass

Master Std PhDStd


Msid
id = ..= .. PhDid = ..
major = ‘AI' major = 'Sec.'
------------------------- -------------------------
BAmajor == 'cs‘
Bamajor 'cs‘ BAmajor = 'cs‘
Acc_Avg = 80.7 BAAvg = 80.7
Master = ‘AI‘
Msavg = 91.3
+ SetMsMajor
+ SetMsID + SetPhdMajor
Ch-1 CS 210 OOP
+ SetPhdID
in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 8
Example of a class
– ……
– Provide other examples of classes in real world?
– Identify their characteristics?
( Rectangle )

gr am
ia
Ld
s UM
s
Cla
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 9
Example of a class
– ……
– Provide other examples of classes in real world?
– Identify their characteristics?
( Rectangle )

General
class

class name  Rectangle


width : int (float)
propertie length : int (float)
s color : RGB (string)

operations + CalculateArea( … ): float


+ SetWidth ( float ): void
+ setLength( float ) : void gr am
ia
Ld
s UM
s
Cla
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 10
Encapsulation
(or Information Hiding)

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 11
What is Encapsulation (or
Information Hiding)?  1
• Encapsulation: is the inclusion-within a program
object-of all the resources needed for the object to
function.
– The methods and the data
•  In OOP the encapsulation is mainly achieved by
creating classes,
– The classes expose public methods and properties
• The main idea of encapsulation is to hide how a class
does its business, while allowing other classes to
make requests of it.
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 12
What is Encapsulation (or
Information Hiding)?  2
• In order to modularize/define the functionality of a
one class that class can uses functions
or properties exposed by another class in many
different ways.
• According to Object Oriented Programming there
are several techniques, where classes can use
these techniques to link with each other.
– Those techniques are named association, aggregation,
and composition.

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 13
The OOP techniques that classes can use to
link with each other.
1- Association
2- aggregation, and
3- composition.

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 14
What is Association?
1- Association

• Association is a (*a*) relationship between two


classes.
• It allows one object instance to cause another to
perform an action on its behalf.
• Association is the more general term that define the
relationship between two classes, 

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 15
What is the difference between Association,
Aggregation, and Composition?
1- Association
• Association is a *has-a* relationship between two
classes where there is no particular ownership in place.
– It is just the connectivity between the two classes.
– When you define a variable of one class in another class, you
enable first to associate functions and properties of the second
class.
• Then again  both Aggregation and Composition are
types of Association.

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 16
What is the difference between Association,
Aggregation, and Composition?
2- Aggregation
• Aggregation is a weak type
of Association with partial ownership.
– For an Aggregation relationship, we use the term *uses* to imply
a weak *has-a* relationship.
– This is weak compared to Composition.
– For example, a school department *uses* teachers. Any
teacher may belong to more than one department. And so, if a
department ceases to exist, the teacher will still exist.

school department
1‫لخاصه‬11‫ ا‬1‫لتعليم‬11‫كز ا‬1‫مثل مرا‬

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 17
What is the difference between Association,
Aggregation, and Composition?
3- Composition
• Composition is a strong type
of Association with full ownership. 
– This is strong compared to the weak Aggregation. 
– For a Composition relationship, we use the term *owns* to
imply a strong *has-a* relationship.
– For example, a department *owns* courses, which means that
the any course's life-cycle depends on the department's life-
cycle. Hence, if a department ceases to exist, the underlying
courses will cease to exist as well.

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 18
What is the difference between Association,
Aggregation, and Composition?
• To summarize
– Whenever there is no ownership in place, we regard such a
relationship as just an Association and we simply use
the *has-a* term, or sometimes the verb describing the
relationship.
– For example, a teacher *has-a* or *teaches* a student. There
is no ownership between the teacher and the student, and
each has their own life-cycle.
– we can say that aggregation is a special kind of an association
and composition is a special kind of an aggregation.

(Association Aggregation Composition)

1- Association  2- Aggregation  3- Composition

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 19
UML : Unified Modeling Language

https://diagrams.visual-
paradigm.com/#diagram:proj=0&type=ClassDiagram&g
allery=/repository/e952006a-41a9-4866-83dd-
8dad5219dc15.xml&name=Company%20Structure

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 20
What is a UML?
• UML, short for Unified Modeling Language.
• It is a standardized modeling language
consisting of an integrated set of diagrams,
developed to help system and software
developers for specifying, visualizing,
constructing, and documenting the artifacts
of software systems, as well as for
business modeling and other non-software
systems.
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 21
What is a UML? Cont.
• The UML is a very important part of developing
object oriented software and the software
development process.
• The UML uses mostly graphical notations to
express the design of software projects.
• Using the UML helps project teams communicate,
explore potential designs, and validate the
architectural design of the software.

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 22
What is a UML? Cont.
• UML has a specific notation for each
terminology in OOP.
• Examples are classes, objects,
relationships, behaviors and attributes
• A set of diagrams are created for each
purpose

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 23
What is a UML? Cont.
• Two types of diagrams
1. Structure Diagrams or Structural Diagrams
2. Behavior Diagrams
=============================================
1- Structure Diagrams : 2- Behavior Diagrams :
Examples of Structural Examples of Behavior
Diagrams are : Diagrams are :
1. Class diagram 1. Use-case diagram
2. Component Diagram 2. Activity Diagram
3. Deployment Diagram 3. State Machine Diagram
4. Object Diagram 4. Sequance Diagram
5. Package Diagram … etc.
… etc.
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 24
Example of a
Genera
UML classclass name  Student
class

id = 20189012345
name = 'Fadi'
Attributes phone = 077112233
or
email = [email protected]
properties
address = 'Irbid – Jordan'
major = 'cs'

operations + SetphoneNumber( … ): int


+ Setemail ( … ): void
+ display (…) : void

gr am
ia
Ld
s UM
s
Cla

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 25
Class Notation
• A class notation consists of three parts:
– Class Name
• The name of the class
– Class Attributes
1. The different properties of a class.
2. The attribute type is shown after the colon
followed by the type.
3. Attributes map onto member variables (data
members) in code.
– Class Operations (Methods)
1. They are services the class provides.
2. The return type of a method is shown after the
colon at the end of the method signature.
3. The return type of method parameters are shown
after the colon following the parameter name.
4. Operations map onto class methods in code.

https://en.wikipedia.org/wiki/Class_diagram
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 26
Class Notation
# Blueprint Student class
class Student:
name= ' add name '
id = 0
major = ' Add the major '

def display(self):
print("Std. Name: " + (self.name))
print("Std. id: " + str(self.id))
print("Std.__major: " + (self.major))

#------ ------
# main code

gr am print(Student.name)
ia print(Student.id)
Ld print(Student.major)
s UM
s
Cla print('\n---------- s1 ----------')

s1 = Student()
s1.name = 'Hadi Al-Kadi'
s1.id = 201890700322
s1.major = 'cs'

s1.display() # operation 1 Print and show the output

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 27
What is a gr am
ia
Class Diagram? Cla
ss UM
Ld

• The class diagram is a central modeling


technique that runs through nearly all
object-oriented methods.
• This diagram describes the types of
objects in the system and various kinds of
static relationships which exist between
them.

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 28
Types of relationships
• There are five principal kinds of
relationships which are important:
– Association .
– Inheritance (or Generalization)
– Aggregation 
– Composition
– Dependency

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 29
Types of relationships
• Association.
– A structural link between two peer classes.
– There is an association between Class1 and Class2
– A solid line connecting two classes

Class Homework :
for example 5 stds in project are worded with a supervisor
Instructor *…….* Project_5Stds

supervisor Project_5Stds

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 30
Types of
relationships
• Inheritance (or
Generalization).
– Represents an "is-a"
relationship.
– SubClass1 and SubClass2 Class Homework Inheritance is Generalization relationshi
are specializations of Super Students *is-a* MasterStds.
Students *is-a* PhDStds.
Class.
Students
– A solid line with a hollow
arrowhead that point from
the child to the parent
class.

Homework #1 :
For std’s : draw the Class Diagram for:
1- Shape *is-a* Circle, Rectangle, Polygon.
2- Documents *is-a* Thesis, Book, References, Magazine. MasterStds PhDStds
Note: Every std should Submit in paper and write the drawing
answers using his pen.
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 31
Types of relationships
• Aggregation.
– A special type of
association. It represents a
"part of" relationship. Aggregation is weak relationship, for example:
School Department *uses* Teachers.
– Class2 is part of Class1.
School_Dep Teachers
– Many instances (denoted
by the *) of Class2 can be
associated with Class1.
Homework #2 :
– Objects of Class1 and
Class2 have separate Window *…….* Shape
lifetimes.
– A solid line with a unfilled Instructor *…….* Student
diamond at the association
end connected to the class
Section *……* Students
of composite.
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 32
Types of relationships
• Composition
– A special type of
aggregation where parts
are destroyed when the Composition is strong relationship, for example:
whole is destroyed. Department *owns* courses.
– Objects of Class2 live Department Courses
and die with Class1.
– Class2 cannot stand by
itself. Homework #3 :
– A solid line with a filled Circle *owns* Point.
diamond at the Home *……..* Rooms.
association connected to
the class of composite
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 33
Types of relationships
• Dependency.
– Exists between two
classes if changes to
Dependency C1 depend on C2, for example:
the definition of one
Teachers ---- Students.
may cause changes to
the other (but not the Teachers Students

other way around).


– Class1 depends on
Class2.
– A dashed line with an
open arrow.

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 34
The above UML diagram shows that a house has exactly
one kitchen, exactly one bath, at least one bedroom (can
have many), exactly one mailbox, and at most one
mortgage
Ch-1 CS 210(zero
OOP in or one).
Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 35
Example of a The Class Diagram? is a central modeling technique
Class that runs through nearly all object-oriented methods.
Diagram This Class Diagram describes the types of objects in the
system and various kinds of static relationships which exist
between them.

Inheritance
or

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 36
Q#1:
For the following diagram, describe the
meaning of the relationship?
Class 1 : Car ……………… Class 2: …. ….

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 37
Draw a UML class diagram for class Ellipse

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 38
Draw a UML class diagram for the relation between
the following two classes:
Professor 'has a' class to teach

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 39
a- What is the type of relationship between class line and class geometrical object?

b- What is the type of relationship between class Sheet and class Drawing objects?

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 40
c- Mention two operations (methods) for class Text?

d- Mention two data members (attributes) for class Document?

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 41
e- How many composition relationships are in this diagram?

f- How many Aggregation relationships are in this diagram?

Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019) 42
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019)
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019)
Ch-1 CS 210 OOP in Python Updated & revised by Dr. Ra’ed M. Al-Khatib (2019)

You might also like