Learn Python OOP: Classes, Objects, Inheritance, and Polymorphism - Textnotes

Learn Python OOP: Classes, Objects, Inheritance, and Polymorphism


Master Python Object-Oriented Programming concepts including classes, objects, methods, inheritance, encapsulation, and special methods with clear examples.

Objective:

Understand classes, objects, and key OOP concepts in Python to write modular, reusable, and organized code.

Topics and Examples:

1. Classes & Objects

Classes are blueprints for creating objects; objects are instances of classes.

Example:


class Person:
pass # Empty class

# Creating an object
person1 = Person()
print(person1) # <__main__.Person object at 0x...>

2. Attributes & Methods

Attributes store data; methods define behavior for the object.

Example:


class Person:
species = "Human" # Class attribute

def greet(self, name): # Method
print(f"Hello {name}, I am a {self.species}")

person1 = Person()
person1.greet("Chinmaya") # Hello Chinmaya, I am a Human

3. __init__() Constructor

The __init__() method initializes object attributes during creation.

Example:


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

def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")

person1 = Person("Chinmaya", 25)
person1.greet()

4. Encapsulation: Private/Public Attributes

Encapsulation restricts access to class attributes for data security.

  1. Public attributes: accessible from anywhere
  2. Private attributes: prefix with __ (double underscore)

Example:


class Person:
def __init__(self, name, age):
self.name = name # Public
self.__age = age # Private

def get_age(self):
return self.__age # Access private attribute

person1 = Person("Chinmaya", 25)
print(person1.name) # Chinmaya
print(person1.get_age()) # 25
# print(person1.__age) # Error: Private attribute

5. Inheritance & Polymorphism

Inheritance allows a class to derive from another class. Polymorphism allows methods to have different behavior in child classes.

Example:


class Person:
def greet(self):
print("Hello from Person")

class Student(Person): # Inherits from Person
def greet(self): # Polymorphism / Method overriding
print("Hello from Student")

person = Person()
student = Student()

person.greet() # Hello from Person
student.greet() # Hello from Student

6. Method Overriding

A child class can override a parent class method to change its behavior. (Covered in the example above under Polymorphism)

Example:


class Person:
def greet(self):
print("Hello from Person")

class Student(Person):
def greet(self): # Overriding parent method
print("Hello from Student")

student = Student()
student.greet() # Hello from Student

7. Special Methods: __str__(), __repr__(), __len__()

Special methods (dunder methods) customize object behavior.

Example:


class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages

def __str__(self):
return f"Book: {self.title}"

def __repr__(self):
return f"Book('{self.title}', {self.pages})"

def __len__(self):
return self.pages

book = Book("Python Programming", 300)
print(book) # Book: Python Programming
print(repr(book)) # Book('Python Programming', 300)
print(len(book)) # 300

This section covers all essential OOP concepts in Python, including classes, objects, encapsulation, inheritance, polymorphism, method overriding, and special methods, with practical examples for better understanding.