Object Oriented Programming
Object Oriented Programming
Entri
elevate
Object Oriented ● Classes and Objects
● Inheritance
Keywords ● Polymorphism
● Encapsulation
Entri
elevate Entri
elevate
OOPS
● Classes: In Python, a class is a blueprint for creating objects. It defines the attributes and
methods that will be available to each object created from the class. Classes are defined
using the "class" keyword.
● Objects: Objects are instances of a class. Each object has its own set of attributes and
methods that are inherited from the class it was created from.
● Attributes: Attributes are the properties of an object that define its state. They can be
accessed and modified using dot notation. For example, a car object might have
attributes such as "color" and "model".
● Methods: Methods are functions that are associated with a class and can be called on
Entriobjects created from that class. They can be used to modify the object's state or perform
some other operation. For example, a car object might have methods such as "start" and
elevate Entri
"stop". elevate
Class and Objects
Entri
elevate
Class and Object Creation
Example:
class Person:
Pass
p=Person()
Entri
elevate Entri
elevate
Self Function
The self variable represents the instance of the object itself. Most object-oriented
languages pass this as a hidden parameter to the methods defined on an object;
Python does not. You have to declare it explicitly. When you create an instance of the
A class and call its methods, it will be passed automatically
Entri
elevate Entri
elevate
Self function
Entri
elevate Entri
elevate
Init function
● The __init__ method is roughly what represents a constructor in Python. When you call
A()Python creates an object for you, and passes it as the first parameter to the __init__ method.
● Any additional parameters (e.g., A(24, 'Hello')) will also get passed as arguments
● Method is called as soon as the object is instantiated.
● Useful to do any initialization you want to do with your object.
● Difference b/w declaration and initialization
● There will be double underscores both at the beginning and at the end.
Entri
elevate Entri
elevate
Init function
Entri
elevate Entri
elevate
Class Variables and Object Variables
● Class Variables:
They are shared. Can be accessed by all instances of that class. There is only one copy
of the class variable. Change in the same by one object results in the change seen by
all the objects of the class.
● Object Variables:
They are owned by each object/instance of the class. It's not shared so change made
Entri
by one is not reflected/seen by the other.
elevate Entri
elevate
Inheritance
Entri
elevate
OOPS
● Inheritance: Inheritance is a way of creating a new class from an existing class. The new
class, called the subclass, inherits all the attributes and methods of the existing class,
called the superclass. The subclass can also add new attributes and methods or override
existing ones.
● Polymorphism: Polymorphism is the ability of an object to take on many forms. In
Python, this is often achieved through method overriding, where a subclass provides its
own implementation of a method that was defined in the superclass.
● Encapsulation: Encapsulation is the practice of hiding the internal workings of an object
Entrifrom the outside world. This is typically achieved in Python by making certain attributes
and methods private, meaning they can only be accessed within the class or by specific
elevate Entri
methods. elevate
Inheritance Types
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hybrid Inheritance
5. Hierarchical Inheritance
Entri
elevate Entri
elevate
Class A Class A Class A Class B Class A
Class C
Multilevel Inheritance
Class A Class B
Entri
elevate Class D
Example Python Code
class Animal:
def __init__(self,name):
self.name=name This is an example for multilevel
class Mammal(Animal):
inheritance, the child class Dog can
def walk(self):
access the functions of Mammal and
print(self.name,"is walking")
the property of class Animal also.
def show(self):
print("Im a mammal")
class Dog(Mammal):
In inheritance if the parent and child
def bark(self): class have functions with same name,
print(self.name, "is barking") then if we call the function with child
def show(self): object it executes the child class
function not the parent. This process
Entri
print("Im a dog")
is known as polymorphism.
elevate
d1=Dog("Jacky")
d1.walk() Entri
d1.bark() elevate
Example Python Code
class A:
l=12 This is an example for multilevel
w=24 inheritance.
class B:
l=13 In this if both the parent class have
w=9 same feature names then child class
h=13 can access only the features of the first
parent class in which it is inherited. In
class C(A,B):
this example c1 can access common
pass values of class A and other values from
c1=C() class B
Entri
print(c1.l)
elevate
print(c1.h) Entri
elevate
THANK YOU
Entri
elevate Entri
elevate