Object-Oriented Programming (OOP)
Learn to structure your code using Classes and Objects.
๐๏ธ Introduction: The "Blueprint" Analogy
Imagine you are an architect designing a house. You draw a Blueprint (Plan). This blueprint is NOT a house; you cannot live in it. But using this one blueprint, you can build thousands of real houses.
In Python:
1. Class: The Blueprint (The design).
2. Object: The Real House (The actual thing built from the design).
3. Attributes: Details like "Color", "Number of Rooms" (Data).
4. Methods: Actions like "Open Door", "Turn on Lights" (Functions).
This style of coding is called Object-Oriented Programming (OOP). It helps organize code by grouping data and behavior together.
๐ฆ Topic 1: Defining Classes & Objects
To create a class, we use the class keyword. By convention, class names use CapitalizedWords (PascalCase).
๐ Syntax:
# Attributes and methods go here
# Indentation is key!
๐ป Example: A Simple Dog Class
๐ก What is 'self'?
self represents the instance of the class. When you have 100 houses, and you say "Paint the door red", self tells the painter which specific house's door to paint.
๐ง Topic 2: The __init__() Constructor
Usually, when you create an object, you want to give it some starting values (like a name or age). The __init__ function is called automatically every time the class is being used to create a new object.
๐ป Example: Customized Objects
โ๏ธ Topic 3: Methods (Object Actions)
Methods are functions that belong to an object. They can perform operations using the object's data (attributes).
๐ป Example: Bank Account
๐จโ๐งโ๐ฆ Topic 4: Inheritance
Inheritance allows us to define a class that inherits all the methods and properties from another class.
Parent Class (Base Class): The class being inherited from (e.g., Animal).
Child Class (Derived Class): The class that inherits (e.g., Dog).
๐ป Example: Animal Kingdom
๐ Module Summary
- Class: The Blueprint/Template.
- Object: The actual instance created from the class.
- __init__: The constructor that runs when object is created.
- self: Refers to the specific object being used.
- Inheritance: Sharing logic between Parent and Child classes.
๐ค Interview Q&A
Tap on the questions below to reveal the answers.
self represents the instance of the class. It allows the code to differentiate between different objects of the same class. For example, if you have two Dog objects, self.name ensures you get the name of the specific dog you asked for.
Yes. Python allows a child class to inherit from multiple parent classes. Example: class Child(Parent1, Parent2):. This is powerful but can be complex (Diamond Problem).
No. It is not mandatory. If you don't write one, Python uses a default empty constructor. However, you almost always use it to set up initial values for your object.
Encapsulation is the bundling of data (attributes) and methods that operate on that data into a single unit (class). It also involves restricting direct access to some of an object's components (using private variables like __balance).