Provides a means of structuring programs so that properties and behaviors are bundles into individual objects.
OOP reflects the real world behavior of how things work
It make visualization easier because it is closest to real world scenarios.
We can reuse the code through inheritance, this saves time, and shrinks our project.
There are flexibility throug polymorphism
Class
A class is the blueprint for the objects created from that class
Each class contains some data definitions(called fields), together with methods to manipulate that data.
when the object is instantiated from the class, an instance variable is created for each filed in the class.
Object
Object are basic run time entities in an object oriented system.
It is an instance of class
we can say that objects are the variables of the type class.
Class & Object Example
In object-oriented programming, a class serves as a blueprint for creating objects. Just like a blueprint for a building specifies its structure, a class specifies the structure and behavior of objects created from it. For example, if a class represents a building with 100 windows, each object instantiated from that class would initially have 100 windows. However, the properties of objects can often be manipulated after instantiation, allowing for dynamic changes to their characteristics
Class components
Data(the attribute about it)
behavior(the mothods)
Method
A python method is like a python function
It mus be called on an object
It ust put it inside a class
A method has a name, and may take parameters and have any a raturn statement.
class and object example
class Car:
def __init__(self, color, brand):
self.color = color
self.brand = brand
def display(self):
print(f"This car is a {self.color} {self.brand}")
# Create objects
mustang = Car("green", "Ford")
prius = Car("red", "Toyota")
golf = Car("blue", "Volkswagen")
# Call the display method
mustang.display()
prius.display()
golf.display()
Mohamad Ibrahim Rifat
12.03.2024
Constructor
A special kind of method we use to initialize instance members of that class
It is used for initializing the instance members when we create the object of a class.
If you create four objects, the class constructor is called four times.
Every class must have a constructor, even if it simply relies on the default constructor.