U-5 OOPS Concept PP Notes
U-5 OOPS Concept PP Notes
Features of oops:
Class
Object
Encapsulation
Abstraction
Inheritance
Polymorphism
Class
The class can be defined as a collection of objects.
It is a logical entity that has some specific attributes and methods.
A class is a blueprint for the object.
A class is a template for objects
A Class in Python is a logical grouping of data and functions.
A class is a collection of objects
Syntax :
1
class classname:
Class body
Object
An object is an instance of a class.
The objector instance contains real data or information.
The object is an entity that has state and behaviour.
Object as collection of both data and functions that operate on that data.
An object is used to allocate the memory.
Each object has own set of data members and member functions.
Syntax:
Objectname=classname()
Encapsulation
Wrapping up of data and method into a single unit is called Encapsulation.
It is used to restrict access to methods and variables.
Encapsulation is a means of bundling together instance variables and methods to form a
given type (class).
Selected members of a class can be made inaccessible (“hidden”) from its clients,
referred to as information hiding .
Information hiding is a form of abstraction.
Abstraction
Abstraction is used to hide internal details and show only functionalities.
It refers to essential information without including the background details.
Inheritance
Deriving a new class from the old class is called inheritance.
Old class is called parent class or super class or base class.
New class is called child class or sub class or derived class.
2
Reusability of coding is the main advantages of inheritance.
coding is the main advantages of inheritance.
Polymorphism
Poly means many and morph means forms.
It means more than one form with the same name
It means one task can be performed in different ways.
There are types of polymorphism
Compile time polymorphism or Static polymorphism
Run time polymorphism or dynamic polymorphism
Method is invoked at compile time is called compile time polymorphism. Ex. Method
overloading
Method is invoked at runtime is called run time polymorphism. Ex. Method overriding
Class Definition: A class specifies the set of instance variables and methods that are
“bundled together” for defining a type of object.
Python class is a blueprint of an object.
Class is a keyword
Syntax:
Class classname:
Variables and functions
3
Object Definition: An object is simply a collection of data (variables) and methods
(functions) that act on those data.
An object is also called an instance of a class
Syntax:
Objectname=classname()
Example: Output:
class ruff: Hello World
def f1(self):
print("Hello World")
ob=ruff()
ob.f1()
Self definition: The self parameter is a reference to the current instance of the class. It
has to be the first parameter of any function in the class. It contains a reference to the
object instance to which the method belongs.
Constructor Definition:
Constructor is to initialize (assign values) to the data members of the class when
an object of class is created.
In Python the init () method is called the constructor and is always called
when an object is created.
Instance variables are initialized in the init () method.
Syntax:
def init (self):
# body of the constructor
4
Example: Output:
class ruff: 10 20
def init (self,a,b):
self.a=a
self.b=b
def f(self):
print(self.a,self.b)
ob=ruff(10,20)
ob.f()
In Python, constructors are special methods used to initialize objects. The most commonly used
constructor is __init__(), but there are a few different types of constructors based on how and when
they are used.
Here’s a breakdown:
1. Default Constructor
A constructor that does not accept any arguments (except self). It initializes objects with default values.
class Demo:
def __init__(self):
obj = Demo()
print(obj.message)
🔹 2. Parameterized Constructor
class Person:
self.name = name
self.age = age
p = Person("Alice", 25)
5
print(p.name, p.age)
Python does not support method overloading by default (like Java or C++). However, you can simulate it using
default arguments or *args, **kwargs.
class Student:
if name:
self.name = name
else:
self.name = "Unknown"
s1 = Student("Tom")
s2 = Student()
class Test:
if len(args) == 1:
self.value = args[0]
else:
t1 = Test(10)
t2 = Test()
You can create alternative constructors using the @classmethod decorator. This is useful when you want to
create an object from different types of data (like a string or dictionary).
6
class Date:
self.day = day
self.month = month
self.year = year
@classmethod
d = Date.from_string("11-04-2025")
TL;DR
7
Great topic! In Python, a class can be used to create multiple objects, each with its own
unique data. Let’s break this down with examples and concepts.
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def show_details(self):
print(f"Brand: {self.brand}, Color: {self.color}")
Class Attributes
➤ Defined inside the class, but outside any instance methods.
Shared by all instances of the class.
Useful for constants or data you want all instances to share.
class Dog:
species = "Canis familiaris" # Class attribute
dog1 = Dog("Buddy")
dog2 = Dog("Max")
8
Data (Instance) Attributes
➤ Defined inside the __init__() or other instance methods using self.
Unique to each instance of the class.
Used to store data that varies from one object to another.
class Dog:
def __init__(self, name):
self.name = name # Instance attribute
self.tricks = [] # Each dog has its own list of tricks
dog1 = Dog("Buddy")
dog2 = Dog("Max")
dog1.tricks.append("sit")
dog2.tricks.append("roll over")
print(dog1.tricks) # ['sit']
print(dog2.tricks) # ['roll over']