You are on page 1of 15

 Python is an object oriented programming language.

 Almost everything in Python is an object, with its


properties and methods.
 A Class is like an object constructor, or a "blueprint"
for creating objects.
To create a class, use the keyword class:
Example :
class MyClass:
x=5
An object consists of :
 State: It is represented by the attributes of an object. It
also reflects the properties of an object.
 Behavior: It is represented by the methods of an object.
It also reflects the response of an object to other objects.
 Identity: It gives a unique name to an object and enables
one object to interact with other objects.
Now we can use the class named MyClass to create
objects:
Example :
class MyClass:
x=5
p1 = MyClass()
print(p1.x)
 All classes have a function called __init__(), which is
always executed when the class is being initiated.
 Use the __init__() function to assign values to object
properties, or other operations that are necessary to do
when the object is being created:
Example:
class Person:
def __init__(self, name, age):
self.name = name
Note: The __init__() function is called automatically
every time the class is being used to create a new object.
 The self parameter is used to access variables that
belongs to the class.
 It does not have to be named self , you can call it
whatever you like, but it has to be the first parameter of
any function in the class:
Example:
class Person:
def __init__(self, name, age):
self .name= name
 Inheritance allows us to define a class that inherits all the
methods and properties from another class.
 Parent class is the class being inherited from, also called
base class.
 Child class is the class that inherits from another class,
also called derived class.
Any class can be a parent class, so the syntax is the same
as creating any other class:
Ex:
class Person:
def __init__(self, fname, lname):
self.firstname = fname continue…
 To create a class that inherits the functionality from
another class, send the parent class as a parameter when
creating the child class:
Example:
class Student(Person):
def __init__(self, fname, lname):

 Python also has a super() function that will make the


child class inherit all the methods and properties from its
parent:
Example:
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
 Single Inheritance: Single inheritance enables a derived
class to inherit properties from a single parent class.
Ex:
class Parent:
def func1(self):

class Child(Parent):
def func2(self):

 Multiple Inheritance: When a class can be derived from


more than one base class.
continue…..
Ex:
class Mother:
def mother(self):

class Father:
def father(self):

class Son(Mother, Father):


def parents(self):

 Multilevel Inheritance: In multilevel inheritance, features of


the base class and the derived class are further inherited into
the new derived class.
continue…..
Ex:
class Grandfather:
def __init__(self, grandfathername):

class Father(Grandfather):
def __init__(self, fathername, grandfathername):

class Son(Father):
def __init__(self,sonname, fathername,
grandfathername):
 Hierarchical Inheritance: When more than one derived
classes are created from a single base .

continue…..
Ex:
class Parent:
def func1(self):

class Child1(Parent):
def func2(self):

class Child2(Parent):
def func3(self):

 Hybrid Inheritance: Inheritance consisting of multiple


types of inheritance is called hybrid inheritance.
continue……
Ex:
class School:
def func1(self):

class Student1(School):
def func2(self):

class Student2(School):
def func3(self):

class Student3(Student1, School):


def func4(self):
 It describes the idea of wrapping data and the methods
that work on data within one unit.
 Public members: Can be access outside the class via
object or everywhere.
 Private members: Can be access only inside the class not
outside the class and not by any base class.
To define a private member prefix the member name
with double underscore “__”.
Ex:
Class A():
def __init__(self, a):
Self. __a = a
continue……
 Protected members: Can be access from within class
and its child class , not outside the class.
 To define a private member prefix the member name
with single underscore “_”.
Ex:
Class A():
Def __init__ (self, a):
Self. _a = a

Note: Python’s private and protected member can be


accessed outside the class through Python name
mangling.
-Mohd Sanabil
--Mohd Uvais

You might also like