0% found this document useful (0 votes)
25 views

Python Oop

Uploaded by

Abhishek M B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python Oop

Uploaded by

Abhishek M B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Python

OOPS
By
Abhishek M B
🞂 ​ 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)
🞂 ​ Allclasses 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.
🞂​ Childclass 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…
🞂​ Tocreate 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):

🞂 ​ HybridInheritance: 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):
🞂 ​ Itdescribes 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.

You might also like