Unit_4_OOP_in_Python
Unit_4_OOP_in_Python
Example:
# define a class
class Bike:
name = ""
gear = 0
# create object of class
bike1 = Bike()
# access attributes and assign new values
bike1.gear = 11
bike1.name = "Mountain Bike"
print(f"Name: {bike1.name}, Gears: {bike1.gear} ")
3. Python Methods: We can also define a function inside a Python class. A Python
function defined inside a class is called a method.
Example: (Practical no 17)
# create a class
class Room:
length = 0.0
breadth = 0.0
# method to calculate area
def calculate_area(self):
print("Area of Room =", self.length * self.breadth)
4. Python Constructors: Constructors are generally used for instantiating an object. The
task of constructors is to initialize(assign values) to the data members of the class when
an object of the class is created. In Python, the __init__() method is called the constructor
and is always called when an object is created.
In Python, there are two types of constructors:
i)Default Constructor: A default constructor is a constructor that takes no arguments. It is
used to create an object with default values for its attributes.
Example:
class Person:
def __init__(self):
self.name = "John"
self.age = 30
person1 = Person()
print(person1.name)
print(person1.age)
rect2 = Rectangle(5)
print(rect2.length, rect2.breadth)
class Department(Employee):
def message(self):
print('This Department class is inherited from Employee')
emp = Employee()
emp.message()
print('------------')
dept = Department()
dept.message()
Syntax: In python, if we want to hide any variable, then we have to use the double
underscore() before the variable name. This makes the class members inaccessible and
private to other classes.
__variablename
Private: When members of the class are private, they can only be accessed by objects of
the same class. It is not accessible outside of the class, not even any child class can access
it. If you try to do it, the Python compiler will give an error message. We use double
underscores (__) before the data member to create a private data member inside the class.
Protected: Protected means the member of a class is only accessible by its inherited or
child class. To create a protected data member inside the class, we use a single
underscore (_) before the data member.
class Authentication:
__username = 'abc' # Private member
_password = 'xyz@123' # Protected member
def creden(self):
print("Username:", self.__username)
print("Password:", self._password)
p = Authentication()
p.creden()
Example 2: If we try to access a private member of a class from outside of a class, then it
will throw an error
class Authentication:
__username = 'abc' # Private member
__password = 'xyz@123' # Private member
def creden(self):
print("Username:", self.__username)
print("Password:", self.__password)
p = Authentication()
print("Username:", self.__username) # Accessing from outside of the class
print("Password:", self.__password)
class sub_class(super_class):
# attributes and method of super_class
# attributes and method of sub_class
a) Single Inheritance: In single inheritance, the child class acquires the properties and
can access all the data members and functions defined in the parent class. A child class
can also provide its specific implementation to the functions of the parent class.
Syntax:
class BaseClass1:
#Body of base class
class DerivedClass(BaseClass1):
#body of derived – class
class Mul:
def Multiplication(self,a,b):
return a*b;
class Derived(Add,Mul):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Addition(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))