Python

Python OOP for Beginners: A Simple Guide to Object-Oriented Programming

Learn Object-Oriented Programming in Python from the basics with simple examples of classes, objects, encapsulation, inheritance, polymorphism, and abstraction.


Python OOP for Beginners

If you have started learning Python, you will eventually come across Object-Oriented Programming (OOP).

At first, terms like class, object, inheritance, encapsulation, polymorphism, and abstraction can sound complicated.

But the basic idea of OOP is actually simple:

OOP is a way of organizing your program using objects that contain data and functions together.

In this article, we will learn OOP in Python step by step, starting from the absolute basics.


What is OOP?

OOP stands for Object-Oriented Programming.

Instead of writing a large program as a collection of unrelated functions and variables, OOP allows us to organize our code around objects.

For example, imagine a student.

A student has some information:

name = "Rahul"
age = 21
course = "Python"

A student can also perform actions:

study()
attend_class()
write_exam()

In OOP, we can combine the student’s data and actions into one object.

This makes large programs easier to organize and maintain.


Why Do We Need OOP?

Before understanding OOP, it helps to understand the problem it solves.

Imagine you are creating a program for a bank.

You might have:

account1_name = "Rahul"
account1_balance = 5000

account2_name = "Priya"
account2_balance = 8000

Then you might create functions such as:

deposit()
withdraw()
check_balance()

As the program becomes larger, managing everything separately becomes difficult.

Instead, we can create a BankAccount object.

account1 = BankAccount("Rahul", 5000)
account2 = BankAccount("Priya", 8000)

Each object can have its own data and behavior.

This is one of the main reasons we use OOP.


Class and Object

These are the two most important concepts to understand first.

What is a Class?

A class is a blueprint for creating objects.

Think about a house blueprint.

The blueprint describes things such as:

Number of rooms
Doors
Windows
Kitchen

But the blueprint itself is not a house.

Similarly, a class defines what an object should contain and what it should be able to do.

Example:

class Student:
    pass

Here, Student is a class.


What is an Object?

An object is an instance of a class.

We create an object from a class like this:

student1 = Student()

Here:

Student → Class
student1 → Object

We can create multiple objects from the same class:

student1 = Student()
student2 = Student()
student3 = Student()

All three objects are created from the same Student class.


The __init__() Method

Usually, an object needs some data when it is created.

For example, every student should have a name and age.

We can use the __init__() method for this.

class Student:

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

Now we can create objects:

student1 = Student("Rahul", 21)
student2 = Student("Priya", 22)

The values are stored inside the objects.

print(student1.name)
print(student1.age)

Output:

Rahul
21

What is self?

self refers to the current object.

For example:

class Student:

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

When we create:

student1 = Student("Rahul", 21)

Python essentially stores:

student1.name = "Rahul"
student1.age = 21

When we create another object:

student2 = Student("Priya", 22)

Python stores:

student2.name = "Priya"
student2.age = 22

So self allows each object to maintain its own data.


Attributes and Methods

An object usually contains two important things:

  • Attributes → data
  • Methods → behavior

For example:

class Student:

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

    def study(self):
        print(self.name, "is studying")

Here:

name → Attribute
age → Attribute
study() → Method

We can use it like this:

student = Student("Rahul", 21)

print(student.name)
print(student.age)

student.study()

Output:

Rahul
21
Rahul is studying

The Four Main Concepts of OOP

The four concepts that you will commonly hear about are:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Let’s understand each one simply.


1. Encapsulation

Encapsulation means keeping data and the methods that work with that data together inside a class, while controlling access to the data when necessary.

Consider a bank account.

We should not allow anyone to directly change the balance.

For example, we don’t want someone to do:

account.balance = -50000

Instead, we can keep the balance private and provide methods to control it.

class BankAccount:

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

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

Now:

account = BankAccount(5000)

account.deposit(2000)

print(account.get_balance())

Output:

7000

Here:

self.__balance

is a private attribute.

The __ tells Python that we don’t want the attribute to be accessed directly in the normal way.

Instead, we use methods such as:

deposit()
get_balance()

This is a simple example of encapsulation.


2. Inheritance

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

Imagine we have a general Vehicle class.

class Vehicle:

    def start(self):
        print("Vehicle started")

Now we create a Car class that inherits from Vehicle.

class Car(Vehicle):
    pass

Now the Car object can use the start() method.

car = Car()

car.start()

Output:

Vehicle started

The relationship is:

Vehicle

  Car

Vehicle is the parent class.

Car is the child class.

The child class can reuse functionality from the parent class.


Adding New Behavior

The child class can also have its own methods.

class Vehicle:

    def start(self):
        print("Vehicle started")


class Car(Vehicle):

    def drive(self):
        print("Car is driving")

Now:

car = Car()

car.start()
car.drive()

Output:

Vehicle started
Car is driving

The Car class inherited start() from Vehicle and added its own drive() method.


3. Polymorphism

The word polymorphism means “many forms”.

In OOP, it means that the same method or interface can behave differently depending on the object.

For example:

class Dog:

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


class Cat:

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

Both classes have a method called:

sound()

But they behave differently.

dog = Dog()
cat = Cat()

dog.sound()
cat.sound()

Output:

Bark
Meow

The method name is the same:

sound()

but the behavior is different.

That is a simple example of polymorphism.


Polymorphism with Inheritance

Polymorphism is especially useful with inheritance.

class Animal:

    def sound(self):
        print("Animal makes a sound")


class Dog(Animal):

    def sound(self):
        print("Dog barks")


class Cat(Animal):

    def sound(self):
        print("Cat meows")

Now:

animals = [Dog(), Cat()]

for animal in animals:
    animal.sound()

Output:

Dog barks
Cat meows

The same:

animal.sound()

produces different results depending on the object.


4. Abstraction

Abstraction means hiding unnecessary implementation details and showing only the essential functionality.

Think about riding a bike.

You know how to:

Start the bike
Accelerate
Brake
Change gears

But you don’t need to understand every internal mechanical detail of the engine to ride it.

That is the basic idea of abstraction.

In Python, we can create abstract classes using the abc module.

from abc import ABC, abstractmethod


class Vehicle(ABC):

    @abstractmethod
    def start(self):
        pass

Here:

Vehicle

is an abstract class.

And:

start()

is an abstract method.

A child class must provide the implementation.

class Car(Vehicle):

    def start(self):
        print("Car starts with a key")

Now:

car = Car()
car.start()

Output:

Car starts with a key

The Vehicle class tells us that a vehicle must have a start() method, but it doesn’t decide exactly how every vehicle should start.

The child class provides the actual implementation.


Putting the Concepts Together

Now let’s create a small example that uses several OOP concepts together.

Imagine we are creating a simple banking system.

We can start with a parent class:

class BankAccount:

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

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

Here we have encapsulation because the balance is private.

Now let’s create a savings account.

class SavingsAccount(BankAccount):

    def add_interest(self):
        print("Interest added to savings account")

SavingsAccount inherits from BankAccount.

So we have inheritance.

We can create an object:

account = SavingsAccount("Rahul", 5000)

account.deposit(1000)

print(account.get_balance())

account.add_interest()

Output:

6000
Interest added to savings account

Here we have already used multiple OOP concepts together:

Class

BankAccount

Object

account

Encapsulation

__balance

Inheritance

SavingsAccount → BankAccount

As the project grows, we can add more account types and behavior.


Why OOP is Useful in Real Projects

OOP becomes especially useful when programs become larger.

For example, imagine building an online shopping application.

You might have classes such as:

User
Product
Cart
Order
Payment
Address

Each class can have its own data and methods.

For example:

class Product:

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

    def display(self):
        print(self.name, self.price)

A Cart class can manage products.

An Order class can manage orders.

A Payment class can handle payments.

Instead of putting everything into one huge Python file with unrelated variables and functions, we organize the application into logical objects.

That makes the program easier to understand, maintain, and extend.


Common Beginner Mistakes

When learning OOP, beginners often make a few mistakes.

1. Confusing Class and Object

Remember:

Class = Blueprint
Object = Actual thing created from the blueprint

Example:

class Student:
    pass

student1 = Student()

Student is the class.

student1 is the object.


2. Forgetting self

When defining instance methods, you normally need self.

Correct:

class Student:

    def study(self):
        print("Studying")

Not:

class Student:

    def study():
        print("Studying")

3. Making Everything Private

Encapsulation does not mean that every attribute must be private.

Use private attributes when you actually need to control access or protect the internal state of an object.


4. Learning Definitions Without Writing Code

OOP is difficult to understand if you only memorize definitions.

Instead of only remembering:

Inheritance = acquiring properties from another class

write a small example:

class Animal:

    def eat(self):
        print("Eating")


class Dog(Animal):
    pass

Then create an object and experiment with it.

The concepts become much easier once you actually use them.


A Simple Way to Learn OOP

If you are learning OOP for the first time, don’t try to master everything in one day.

A good order is:

1. Class

2. Object

3. __init__()

4. self

5. Attributes

6. Methods

7. Encapsulation

8. Inheritance

9. Polymorphism

10. Abstraction

11. Build a small project

Once you understand these concepts, the best next step is to build something yourself.

For example:

  • Bank Account System
  • Student Management System
  • Library Management System
  • Vehicle Management System

You don’t need a huge project.

A small project that uses multiple OOP concepts is often much better for learning.


Final Takeaway

Object-Oriented Programming can look complicated when you first see terms such as inheritance, polymorphism, abstraction, and encapsulation.

But the basic idea is straightforward.

You create classes that describe what an object should contain.

You create objects from those classes.

You use attributes to store data and methods to define behavior.

Then you can use:

  • Encapsulation to control and protect data.
  • Inheritance to reuse functionality.
  • Polymorphism to allow the same interface to behave differently.
  • Abstraction to hide unnecessary implementation details.

The most important thing is not to memorize these definitions.

Write small programs, experiment with the code, make mistakes, and build a small project.

That is when OOP starts to make sense.