Skip to main content

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

Output

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

Output

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

Output

4. self Keyword

self refers to the current object of the class. It is used to access instance variables and instance methods.

Example

Output

5. Instance Variables

Instance variables belong to an object. Every object has its own separate copy.

Example

Output

6. Instance Methods

Instance methods work with object data and always receive self as the first parameter.

Example

Output

7. Class Variables

Class variables belong to the class itself and are shared among all objects.

Example

Output

8. Class Methods (@classmethod)

Class methods work with class variables. They receive cls instead of self.

Example

Output

9. Static Methods (@staticmethod)

Static methods do not use instance variables or class variables. They behave like normal functions inside a class.

Example

Output

10. Encapsulation

Encapsulation combines data and methods into a single class while controlling access to the data.

Example

Output

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

Output

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

Output

15. Method Overriding

Method overriding occurs when a child class provides its own version of a parent class method.

Example

Output

16. Operator Overloading

Operator overloading allows operators such as +, -, and * to work with user-defined objects.

Example

Output

17. Abstraction

Abstraction hides implementation details and exposes only the essential features.

Example

Output

18. super() Function

super() is used to call the parent class constructor or methods.

Example

Output

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

Output

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