Oops concept in Python
Organise the code using classes and objects
structured, reusable and maintainable
Class student:
pass
Creating the objects
instance of the class
s1= student()
constructor
__init__() → which initiallise the object.
class student:
pass
Opps concept :
- Encapsulation
- Abstraction
- Inheritance
- polymorphism
Inheritance:
# multiple Class Inheritance
class Parent:
def speak1(self):
print("Father speaking")
class Parent2:
def speak2(self):
print("Mother speaking")
class Child(Parent, Parent2):
def speak(self):
print("Child speaking")
# Object creation
c= Child()
c.speak1()
c.speak2()
c.speak()
in above case 2 parent class are created and one child class is created ,
Get Arunram’s stories in your inbox
Join Medium for free to get updates from this writer.
respective classes are defined using constructor and called using the object.(Multiple inheritance)
# Multilevel Class Inheritance
class Parent:
def speak1(self):
print("Grand Father speaking")
class Parent2(Parent):
def speak2(self):
print("Father speaking")
class Child(Parent2):
def speak(self):
print("Child speaking")
# Object creation
c= Child()
c.speak1()
c.speak2()
c.speak()Hierarchical inheritance:
# Class Inheritance
class Parent:
def speak1(self):
print("Father speaking")
class Child1(Parent):
def speak2(self):
print("Child1 speaking")
class Child2(Parent):
def speak(self):
print("Child2 speaking")
# Object creation
c1= Child1()
c2= Child2()
c1.speak2()
c1.speak1()
c2.speak()
c2.speak1()Polymorphism:
overloading:
overloading is not possible directly in python instead we can achieve it using *args in function in class.
class overload1():
def add(self,a, *args):
return a+sum(args)
class overload2(overload1):
def subtract(self,a, *args):
return a-sum(args)
class overloade3(overload2):
def multiply(self,a, *args):
result = a
for i in args:
result *= i
return result
c = overloade3()
print(c.subtract(2,3,4))
print(c.add(2,3,4))
print(c.multiply(2,3,4))over riding :
if we are using the same function name in parent and child clause
while calling using object it will call child function only to override it we use super().function() to call the parent class function.
class parent():
def add(self):
print("parent class add method")
class child(parent):
def add(self):
super().add() #this overide the child class method and call the parent class method
print("child class add method")
c = child()
c.add()
encapsulation
class bank():
def __init__(self,name,balance,amount):
self.__name = name
self.__balance = balance
self.__amount = amount
def get_name(self):
return self.__name
def get_deposit(self):
self.__balance += self.__amount
return self.__balance
def get_withdraw(self):
if self.__balance >= self.__amount:
self.__balance -= self.__amount
return self.__balance
else:
return "Insufficient balance"
class child_bank(bank):
def get_withdraw(self):
if self._bank__balance >= self._bank__amount:#### name-mangled _bank__
self._bank__balance -= self._bank__amount
return self._bank__balance
else:
return "Insufficient balance"
p = child_bank("Arun", 1000, 500)
print(p.get_name())
print("Balance after withdrawal:", p.get_withdraw())
print("Balance after deposit:", p.get_deposit())abstract method:
@abstractmethod
it is used to modified the inherited parent method.
Errors caught immediately at instantiation.
if a parent class mention all the methods as abstractmethod then it is called as interface.
from abc import ABC, abstractmethod
class RBI(ABC):
@abstractmethod
def create_account(self,*args):
pass
class SBI(RBI):
def create_account(self,*args):
amount = args[0]
if amount > 1000:
print("SBI account created successfully")
else:
print("SBI account creation failed. Minimum amount required is 1000")
class HDFC(RBI):
def create_account(self,*args):
print("HDFC account created successfully")
s = SBI()
h = HDFC()
s.create_account(500)
h.create_account()


