Introduction To OOP
Introduction To OOP
1. What is OOP?
Object-Oriented Programming (OOP) is a way of thinking about and organizing code. Instead of
just writing a bunch of instructions for the computer to follow, we can create "objects" that
represent things in the real world.
2. What is an Object?
An object is like a thing or an item in the real world. For example, a car, a cat, or a pencil can all
be objects. In programming, objects have:
o Attributes (what the object is or has): For example, a car has a color, speed, and model.
o Methods (what the object can do): For example, a car can drive, stop, and honk.
3. What is a Class?
A class is like a blueprint for creating objects. Think of it as a cookie cutter that can be used to
make lots of cookies (objects) of the same shape. For example, a Car class can be used to create
different car objects.
Explanation:
o __init__: This is a special method that runs when we create a new object. It sets
up the object's attributes.
o self: This keyword refers to the object itself. We use it to access the object's
attributes and methods.
o color and model are attributes of the Car class.
o drive and stop are methods of the Car class.
5. Creating Objects from a Class
Once we have a class, we can create objects from it:
Explanation:
o my_car and your_car are objects created from the Car class.
o We can call the drive and stop methods on these objects to see what they do.
Organization: OOP helps organize code better by grouping related data and functions together.
Reusability: Once we create a class, we can create multiple objects from it without rewriting the
same code.
Flexibility: OOP makes it easier to add new features and change how things work without
breaking other parts of the code.