OOPS Concepts
OOPS Concepts
Classes
A class will let you bundle together the behavior and state of an object.
The following points are worth notable when discussing class bundles:
• The word behavior is identical to function – it is a piece of code that
does something (or implements a behavior).
• The word state is identical to variables – it is a place to store values
within a class.
• When we assert a class behavior and state together, it means that a
class packages functions and variables.
def greet(self):
print("Hello this is a greet method")
def leave(self):
print("Hello this is a leave method")
a = People()
print(a.legs)
a.greet()
a.leave()
b = People()
print(b.hands)
b.greet()
b.leave()
Output
def first(self):
print("hello, World")
obj = MyClass()
print(obj.var)
obj.first()
Output
Encapsulation
Encapsulation is one of the fundamentals of OOP. OOP enables us to hide the
complexity of the internal working of the object which is advantageous to the
developer in the following ways:
• Simplifies and makes it easy to understand to use an object without
knowing the internals.
• Simplifies and makes it easy to understand to use an object without
knowing the internals.
Encapsulation provides us the mechanism of restricting the access to some of
the object’s components, this means that the internal representation of an
object cannot be seen from outside of the object definition. Access to this data
is typically achieved through special methods: Getters and Setters.
class Human:
def setGender(self, g):
self.gender = g
def getGender(self):
return self.gender
obj1 = Human()
obj1.setGender("Male")
print(obj1.getGender())
obj2 = Human()
obj2.setGender("Female")
print(obj2.getGender())
Output
INIT Constructor
The __init__ method is implicitly called as soon as an object of a class is
instantiated.
This will initialize the object.
obj1 = Human()
The line of code shown above will create a new instance and assigns this object
to the local variable obj1.
The instantiation operation, that is calling a class object, creates an empty
object. Many classes like to create objects with instances customized to a
specific initial state. Therefore, a class may define a special method named
‘__init__()‘ as shown:
def __init__(self):
self.legs = 2
self.hands = 2
The __init__() method can have single or multiple arguments for a greater
flexibility. The init stands for initialization, as it initializes attributes of the
instance. It is called the constructor of a class.
def __init__(self, name):
self.legs = 2
self.hands = 2
self.name = name
def displayInfo(self):
print(f"My name is {self.name} and I have {self.legs}
legs and {self.hands} hands")
obj1 = Human("Elon")
obj2 = Human("Akshay")
obj1.displayInfo()
obj2.displayInfo()
Output
class DerivedClass(BaseClass):
Body of derived class
h = Human()
h.getHuman()
s = Student()
s.getHuman()
s.getStudent()
Output
We first created a class called Human. Later we created another class called
Student and called the Human class as an argument. Through this call we get
access to all the data and attributes of Human class into the Student class.
Because of that when we try to get the getHuman() method from the Student
class object we created earlier possible.
Inheritance Examples
Output
class Cat(Animal):
def speak(self):
print("I am meowing")
class Dog(Animal):
def speak(self):
print("I am barking")
c = Cat()
d = Dog()
a = Animal()
a.speak()
c.speak()
d.speak()
Output
As we can see the code, speak method is in all the classes but has different
forms depending upon the class it is in.
Python itself have classes that are polymorphic. Example, the len() function can
be used with multiple objects and all return the correct output based on the
input parameter.