DEV Community

Aruna Arun
Aruna Arun

Posted on

Day 16: Python programming

Object-Oriented Programming (OOP)

OOP (Object-Oriented Programming) is a programming style that uses objects and classes.
Python is an object-oriented language.

1. What is a Class?
A class is a blueprint or template.
Example:
class Student:
pass

2. What is an Object?
Object = instance of a class.
s1 = Student()
s2 = Student()

3. __init__() Constructor
Constructor = automatic function that runs when object is created.
class Student:
def init(self, name, age):
self.name = name
self.age = age
s = Student("Arun", 21)
print(s.name)
print(s.age)

4. Self Keyword
self refers to current object.
Example:
class A:
def show(self):
print("Hello")

5. Object Methods
class Car:
def init(self, brand):
self.brand = brand

def start(self):
    print(self.brand, "started")
Enter fullscreen mode Exit fullscreen mode

c = Car("BMW")
c.start()

6. Types of Variables in Class
Instance variable
Belongs to object → self.var
Class variable
Common for all objects.
class Student:
college = "ABC College" # class variable
def init(self, name):
self.name = name # instance variable

7. Types of Methods

Method Type Defined As Access
Instance method def method(self): object
Class method @classmethod def method(cls): class
Static method @staticmethod def method(): class/object

Example
class Demo:
def fun1(self): # instance method
print("Instance method")

@classmethod
def fun2(cls):                   # class method
    print("Class method")

@staticmethod
def fun3():                      # static method
    print("Static method")
Enter fullscreen mode Exit fullscreen mode

d = Demo()
d.fun1()
Demo.fun2()
Demo.fun3()

8. Encapsulation
Hiding data using:

  • _protectedVar
  • __privateVar Private variable example: class Bank: def init(self): self.balance = 1000 # private def show(self): print(self.balance) b = Bank() b.show()

9. Getters and Setters
Used to access private variables safely.
class Person:
def init(self):
self.age = 20
def get_age(self):
return self.
age
def set_age(self, a):
self.__age = a
p = Person()
print(p.get_age())
p.set_age(25)
print(p.get_age())

10. Delete Object and Attributes
class Test:
x = 10
t = Test()
del t.x # delete attribute
del t # delete object

11. Special Methods (Magic / Dunder methods)

Method Use
__init__() constructor
__str__() string representation
__len__() length
__add__() operator overloading

Example:
class A:
def str(self):
return "This is object A"
a = A()
print(a)

12. Creating Multiple Objects
class Dog:
def init(self, name):
self.name = name
d1 = Dog("Tommy")
d2 = Dog("Sheru")

13. OOP Real-Time Example
Mobile class:
class Mobile:
def init(self, brand, price):
self.brand = brand
self.price = price
def details(self):
print("Brand:", self.brand)
print("Price:", self.price)
m = Mobile("Samsung", 25000)
m.details()

14. Interior Program – Object Count
class Counter:
count = 0
def init(self):
Counter.count += 1
for i in range(5):
Counter()
print("Objects created:", Counter.count)

Top comments (0)