Python Oop
Python Oop
Python Oop
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):
class Child(Parent):
def func2(self):
class Father:
def father(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):
class Student1(School):
def func2(self):
class Student2(School):
def func3(self):