DEV Community

Cover image for Object-Oriented Programming (OOP) in Python – Day 14
augustineowino357-design
augustineowino357-design

Posted on • Edited on

Object-Oriented Programming (OOP) in Python – Day 14

Object-Oriented Programming (OOP) in Python

In the previous lesson, we learned about Modules and Packages and how they help organize and reuse Python code efficiently. Today, we will learn about Object-Oriented Programming (OOP), one of the most powerful programming approaches in Python.

Object-Oriented Programming helps programmers structure applications using objects and classes, making programs more organized, reusable, and scalable.


What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming approach that organizes code using:

  • Classes
  • Objects

OOP models real-world entities such as:

  • Students
  • Cars
  • Employees
  • Bank Accounts

Each object contains:

  • Attributes (Data)
  • Methods (Functions)

Why OOP is Important

OOP helps programmers:

  • Organize code properly
  • Reuse code efficiently
  • Build scalable applications
  • Improve security
  • Simplify maintenance
  • Represent real-world systems

Most modern software applications use Object-Oriented Programming.


What is a Class?

A Class is a blueprint for creating objects.

It defines:

  • Variables
  • Functions
  • Behaviors

Syntax

class ClassName:
    # class body
Enter fullscreen mode Exit fullscreen mode

Creating a Class

Example

class Student:
    pass
Enter fullscreen mode Exit fullscreen mode

This creates an empty class named "Student".


What is an Object?

An Object is an instance of a class.

Objects use the structure defined inside the class.

Example

class Student:
    pass

student1 = Student()

print(student1)
Enter fullscreen mode Exit fullscreen mode

The init() Method

The "init()" method is called automatically when an object is created.

It initializes object attributes.

Example

class Student:

    def __init__(self, name, age):
        self.name = name
        self.age = age
Enter fullscreen mode Exit fullscreen mode

Creating Objects with Attributes

Example

class Student:

    def __init__(self, name, age):
        self.name = name
        self.age = age

student1 = Student("Augustine", 21)

print(student1.name)
print(student1.age)
Enter fullscreen mode Exit fullscreen mode

Output

Augustine
21
Enter fullscreen mode Exit fullscreen mode

Understanding self

The "self" keyword refers to the current object.

It allows objects to access:

  • Attributes
  • Methods

Example

class Car:

    def __init__(self, brand):
        self.brand = brand
Enter fullscreen mode Exit fullscreen mode

Object Methods

Methods are functions defined inside a class.

Example

class Student:

    def __init__(self, name):
        self.name = name

    def greet(self):
        print("Hello", self.name)

student1 = Student("Augustine")

student1.greet()
Enter fullscreen mode Exit fullscreen mode

Output

Hello Augustine
Enter fullscreen mode Exit fullscreen mode

Modifying Object Properties

Object properties can be changed after object creation.

Example

class Student:

    def __init__(self, name):
        self.name = name

student1 = Student("Augustine")

student1.name = "Brian"

print(student1.name)
Enter fullscreen mode Exit fullscreen mode

Output

Brian
Enter fullscreen mode Exit fullscreen mode

Deleting Object Properties

Python allows deleting object properties using "del".

Example

class Student:

    def __init__(self, name):
        self.name = name

student1 = Student("Augustine")

del student1.name
Enter fullscreen mode Exit fullscreen mode

Inheritance in Python

Inheritance allows one class to inherit properties and methods from another class.

Example

class Person:

    def __init__(self, name):
        self.name = name

class Student(Person):
    pass

student1 = Student("Augustine")

print(student1.name)
Enter fullscreen mode Exit fullscreen mode

Output

Augustine
Enter fullscreen mode Exit fullscreen mode

Polymorphism in Python

Polymorphism allows methods with the same name to behave differently.

Example

class Dog:
    def sound(self):
        print("Bark")

class Cat:
    def sound(self):
        print("Meow")

dog = Dog()
cat = Cat()

dog.sound()
cat.sound()
Enter fullscreen mode Exit fullscreen mode

Output

Bark
Meow
Enter fullscreen mode Exit fullscreen mode

Encapsulation in Python

Encapsulation restricts direct access to certain data.

Example

class BankAccount:

    def __init__(self, balance):
        self.__balance = balance

account = BankAccount(5000)
Enter fullscreen mode Exit fullscreen mode

The double underscore "__" makes the attribute private.


Advantages of OOP

Object-Oriented Programming helps applications:

  • Become modular
  • Improve code reuse
  • Enhance security
  • Simplify debugging
  • Handle large projects efficiently

Most enterprise applications rely heavily on OOP principles.


Real-World Applications of OOP

OOP is used in:

  • Banking Systems
  • School Management Systems
  • Mobile Applications
  • Web Applications
  • Game Development
  • Artificial Intelligence Systems

Conclusion

Object-Oriented Programming is one of the most important concepts in Python programming because it helps developers build organized, reusable, and scalable applications.

Understanding Classes, Objects, Inheritance, and Polymorphism is essential for becoming a professional Python developer.

Practice creating your own classes and objects to strengthen your Object-Oriented Programming skills.

Python #PythonForBeginners #CodingJourney #LearnPython

Top comments (0)