Introduction to OOP
Object-Oriented Programming (OOP) is a programming approach that organizes programs using classes and objects. It models real-world entities, making code reusable, modular, secure, and easier to maintain.1. Class
A class is a blueprint or template used to create objects. It defines the data (attributes) and behavior (methods) that objects will have.Example
2. Object
An object is an instance of a class. It represents a real entity and can access the class’s variables and methods.Example
3. Constructor (__init__)
A constructor is a special method that is automatically called whenever an object is created. It is mainly used to initialize object variables.
Example
4. self Keyword
self refers to the current object of the class. It is used to access instance variables and instance methods.
Example
5. Instance Variables
Instance variables belong to an object. Every object has its own separate copy.Example
6. Instance Methods
Instance methods work with object data and always receiveself as the first parameter.
Example
7. Class Variables
Class variables belong to the class itself and are shared among all objects.Example
8. Class Methods (@classmethod)
Class methods work with class variables. They receive cls instead of self.
Example
9. Static Methods (@staticmethod)
Static methods do not use instance variables or class variables. They behave like normal functions inside a class.
Example
10. Encapsulation
Encapsulation combines data and methods into a single class while controlling access to the data.Example
11. Access Modifiers
Public
Public members can be accessed from anywhere.Protected
Protected members are indicated using a single underscore (_). They are intended for internal use and subclasses.
Private
Private members use double underscores (__) and cannot be accessed directly outside the class.
12. Inheritance
Inheritance allows a child class to reuse the properties and methods of a parent class.Example
13. Types of Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Combination of two or more inheritance types.14. Polymorphism
Polymorphism allows the same method name to perform different actions depending on the object.Example
15. Method Overriding
Method overriding occurs when a child class provides its own version of a parent class method.Example
16. Operator Overloading
Operator overloading allows operators such as+, -, and * to work with user-defined objects.
Example
17. Abstraction
Abstraction hides implementation details and exposes only the essential features.Example
18. super() Function
super() is used to call the parent class constructor or methods.
Example
19. Magic (Dunder) Methods
Magic methods begin and end with double underscores. They define the behavior of objects.Common Magic Methods
__init__()– Constructor__str__()– String representation__len__()– Returns object length__add__()– Operator overloading__repr__()– Official object representation
Example
20. Object Introspection (Additional Important Topic)
Object introspection helps inspect an object’s type, attributes, and inheritance.Common Functions
type()isinstance()issubclass()hasattr()getattr()setattr()
Example
21. Four Pillars of OOP
Encapsulation
Wrapping data and methods into a single class while restricting direct access.Abstraction
Hiding implementation details and exposing only essential functionality.Inheritance
Creating a new class from an existing class to reuse code.Polymorphism
Using the same interface or method name with different implementations.Advantages of OOP
- Code reusability
- Better maintainability
- Easy debugging
- Modular programming
- Data security through encapsulation
- Real-world problem modeling
- Improved scalability