0% found this document useful (0 votes)
51 views8 pages

347 862933 U5 OOPSConcept PP Notes

Uploaded by

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

347 862933 U5 OOPSConcept PP Notes

Uploaded by

a2527937485
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOPS Concept in Python

Definition: Object-oriented programming (OOP) is a method of structuring a


program by bundling related properties and behaviors into individual objects.
Object Oriented means directed towards objects.
Python is an Object Oriented programming (OOP).
It is a way of programming that focuses on using objects and classes to design
and build applications.
It is used to design the program using classes and objects.
Advantages of oops:
It is faster
It is easy to execute
It provides a clear structure for the programs
Easy to maintain, modify and debug
It is used to create full reusable applications with less code and
shorter development time

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
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.

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.
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

1.%2 Fundamental concepts


1.1.%3 Class

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

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

Call the variable and function in a class using the following

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.

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):
self.message = "Hello from default constructor"

obj = Demo()
print(obj.message)

2. Parameterized Constructor
A constructor that takes arguments to initialize an object with custom values.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p = Person("Alice", 25)
print(p.name, p.age)

3. Constructor Overloading ( Not directly supported like in other languages)


Python does not support method overloading by default (like Java or C++). However, you can
simulate it using default arguments or *args, **kwargs.
Example using default values:
class Student:
def __init__(self, name=None):
if name:
self.name = name
else:
self.name = "Unknown"

s1 = Student("Tom")
s2 = Student()
Example using *args:
class Test:
def __init__(self, *args):
if len(args) == 1:
self.value = args[0]
else:
self.value = "No value"

t1 = Test(10)
t2 = Test()
4. Class Method Constructor (Alternative Constructor)
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).

class Date:
def __init__(self, day, month, year):
self.day = day
self.month = month
self.year = year

@classmethod
def from_string(cls, date_str):
day, month, year = map(int, date_str.split('-'))
return cls(day, month, year)

d = Date.from_string("11-04-2025")

TL;DR

Constructor Type Description


Default Constructor No arguments
Parameterized Accepts arguments
Constructor
Overloaded Simulated with default args or *args
Constructor
Class Method Alternative constructor via
Constructor @classmethod
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.
Defining a Class and Creating Multiple Objects
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}")

car1 = Car("Toyota", "Red")


car2 = Car("BMW", "Black")
car3 = Car("Tesla", "White")

car1.show_details() # Output: Brand: Toyota, Color: Red


car2.show_details() # Output: Brand: BMW, Color: Black
car3.show_details() # Output: Brand: Tesla, Color: White

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

def __init__(self, name):


self.name = name # Instance (data) attribute

dog1 = Dog("Buddy")
dog2 = Dog("Max")

print(dog1.species) # Canis familiaris


print(dog2.species) # Canis familiaris

Dog.species = "Canis lupus familiaris" # Change at the class level

print(dog1.species) # Canis lupus familiaris


print(dog2.species) # Canis lupus familiaris

Change at class level affects all instances unless overridden.

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']

Each instance has its own copy of data attributes.

You might also like