Lecture 2
Lecture 2
Omar Yahya
Lecture 2
Encapsulation
The feature of wrapping up data (attributes) and methods associated with that data into a single
unit is called encapsulation in Python. In any other programming languages or Python, this unit
is a class.
For example, scooter parts like engine, brakes, wheels, and methods like start(), stop(),
accelerate() encapsulate together to form a scooter.
Similarly, a class is an example of encapsulation that combines data and methods together into a
single unit and hides their internal details from outside the world.
Another real-time example of encapsulation is a capsule. Essentially, a capsule encapsulates
several combinations of medicine. If combinations of medicine represent variables and methods,
then the capsule will act as a class and the entire process refers to encapsulation as shown in the
below figure.
1
OOP in Python Eng. Omar Yahya
Benefits of Encapsulation
• Data hiding:
Encapsulation helps to maintain the integrity of an object's data by preventing external
modification.
• Easier to understand:
It reduces code complexity by hiding implementation details, making code easier to understand
and maintain.
• Code reusability:
Encapsulation enables code reuse by creating a public interface for a class that can be used by
other parts of the program.
• Improved maintainability:
It provides better control over the behavior of an object, as changes can be made to the
implementation without affecting the rest of the program.
• Better control over class attributes:
Encapsulation helps to prevent naming conflicts by restricting the scope of variables and methods
to the class where they are defined.
Abstraction in Python
Abstraction is used to hide the internal functionality of the function from the users. The users only
interact with the basic implementation of the function, but inner working is hidden. User is familiar
with that "what function does" but they don't know "how it does."
In simple words, we all use the smartphone and very much familiar with its functions such as
camera, voice-recorder, call-dialing, etc., but we don't know how these operations are happening
in the background. Let's take another example - When we use the TV remote to increase the
volume. We don't know how pressing a key increases the volume of the TV. We only know to press
the "+" button to increase the volume.
That is exactly the abstraction that works in the object-oriented concept.
2
OOP in Python Eng. Omar Yahya
Class Diagram
Class diagrams are a type of UML (Unified Modeling Language) diagram used in software
engineering to visually represent the structure and relationships of classes within a system i.e. used
to construct and visualize object-oriented systems.
Class: ITEM
Attribute:
Number
Cost
……….
Methods:
SetData()
GetData()
……….
3
OOP in Python Eng. Omar Yahya
4
OOP in Python Eng. Omar Yahya
Data Types
• Primitive Data Types:
Primitive data types are the most basic types of data. They are predefined by the programming
language and represent simple values. Primitive data types typically have a fixed size in memory,
and their values are stored directly. Ex: Byte, Int, Float, Double, Char, Boolean.
• Non-Primitive Data Types (User):
Non-primitive data types are more complex and can be composed of multiple primitive data types.
They are also known as reference types because they store references to the actual data rather than
the data itself. Non-primitive types are created by the programmer or provided by the language as
classes or interfaces. Ex: Class.
Classes
In Object-Oriented Programming (OOP) in Python, a class is a blueprint for creating objects. A
class encapsulates data (attributes) and behavior (methods) into a single entity, making it easier to
manage and manipulate data in a structured way.
• Attributes: The properties or data members of a class. They represent the state of an object.
Attributes can be variables that store data.
• Methods: Methods are functions defined within a class. They represent the behavior of the
objects. Methods can perform operations on the data.
To create a class in python we use the class key word followed by the class name. Class name
should generally follow the CamelCase convention:
• Class names should start with an uppercase letter.
• If the class name consists of multiple words, each word should start with an uppercase
letter, and there should be no underscores between the words.
• Class names should be descriptive and meaningful, reflecting the purpose or nature of the
class.
Defining a Class:
A class is defined using the class keyword, followed by the class name and a colon. The class
body contains attributes and methods.
class ClassName:
# Define Attribute and Methods here.(Class body)
...
5
OOP in Python Eng. Omar Yahya
Example:
class Car:
brand = "Toyota" # class attribute.
Objects
An object is an instance of a class. A class serves as a blueprint for creating objects, defining the
structure and behavior that the objects created from it will have. Objects are the central concept
in OOP, allowing programmers to model real-world entities in code.
After creatin the class (template) we will need to create an instance of it (object) to be able to
access its attributes. We use the dot ( . ) operator to access attributes and methods:
Example 1:
A Python program that prints the name on the screen using a class.
class Student:
name = "Omar Yahya"
Example 2:
Create a class that represents a student in the class and contains two variables: the student's name
and his age.
class Student:
name = "Ahmed"
age = 30
6
OOP in Python Eng. Omar Yahya
Example 3:
A program to create a simple class representing a phone containing two variables: the phone's
brand and its price.
class Phone:
brand = "Samsung"
price = 1000
7
OOP in Python Eng. Omar Yahya
Example 4:
class Student:
name = "Ahmed"
age = 30
# Modify variables
student1.name = "Mohammed"
student1.age = "20"