OBJECT:
EVERYTHING WE SEEN IN REAL WORLD IS KNOWN AS KNOWN AS OBJECT.
OBJECT has PROPERTIES/ATTRIBUTES and ACTIONS/METHODS to perform.
CLASS:
BLUEPRINT of the object is called Class.
Class has multiple objects.
Instances of class referred as object.
Self is the parameter that tells this is the current object that we using.
#Creating a Class
class MRV:
id = 4
name= "ECE"
def display(self):
print(self.id,self.name)
The problem with this class is if I create multiple objects all has same id and name
If I want to different objects must have different id and name
Constructor:
It is method to initialize the objects of a class
'''
#Creating Object
s1=MRV()
s1.display()
#constructor:
class MRV:
def __init__(self, id, name):
self.id = id
self.name= name
def display(self):
print(self.id,self.name)
#Creating Object
s2=MRV(5,"CSE")
s2.display()
s3=MRV(66,"AIML")
s3.display()
'''
#if want to know the count that how many objects are created
class MRV:
count=0
def __init__(self, id, name):
self.id = id
self.name= name
MRV.count=MRV.count+1
def display(self):
print(self.id,self.name)
s1=MRV(4,"ECE")
s2=MRV(5,"CSE")
s3=MRV(66,"AIML")
print("No.of Branches",MRV.count)
Encapsulation:
Wrapping data and the methods togethers
It can be done using private methods: created using underscore as the prefix either single or double
Abstraction:
Showing only relevant information
#Encapsulation and Abstraction
class MRV:
def __init__(self):
self.id=100
def display(self):
print("ID:",self.id)
s1=MRV()
s1.display()
s1.id=101
s1.display()
Inheritance:
Process of creating a new class using existing class
#Inheritance
class animal:
def speak(self):
print("Animal can't speak")
class dog(animal):
def bark(self):
print("dog is barking")
d=dog()
d.bark()
d.speak()
#Multilevel Inheritance
class animal:
def speak(self):
print("Animal can't speak")
class dog(animal):
def bark(self):
print("dog is barking")
class childdog(dog):
def eat(self):
print("Child dog is eating")
d=childdog()
d.eat()
d.bark()
d.speak()
#Multiple Inheritance
class animal:
def speak(self):
print("Animal can't speak")
class dog:
def bark(self):
print("dog is barking")
class childdog(dog,animal):
def eat(self):
print("Child dog is eating")
d=dog()
d.bark()
d.speak()
Polymorphism:
Single name, many works
This process is also refer as overriding.
Method overloading: not possible in python
#Polymorphism
class rbi:
def getroi(self):
return 10
class sbi(rbi):
def getroi(self):
return 6
class icici(rbi):
def getroi(self):
return 8
b1=rbi()
b2=sbi()
b3=icici()
print("ROI of RBI",b1.getroi())
print("ROI of SBI",b2.getroi())
print("ROI of ICICI",b3.getroi())