0% found this document useful (0 votes)
5 views14 pages

Oops Notes

Object-Oriented Programming (OOP) is a programming paradigm centered around objects and classes, where classes serve as blueprints for creating objects. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which enhance code reusability and security. OOP also utilizes special methods, data hiding, and method resolution order to manage object behavior and relationships.

Uploaded by

storagemathura
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)
5 views14 pages

Oops Notes

Object-Oriented Programming (OOP) is a programming paradigm centered around objects and classes, where classes serve as blueprints for creating objects. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, which enhance code reusability and security. OOP also utilizes special methods, data hiding, and method resolution order to manage object behavior and relationships.

Uploaded by

storagemathura
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

What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm based on objects and classes.

 Class → Blueprint/template
 Object → Instance of a clas

class Car:

pass

c1 = Car() # object

Class & Object

Class

A class defines attributes (variables) and methods (functions).

class Student:

name = "Kunal"

Object

s1 = Student()

print([Link])
Constructor (__init__)
 Automatically called when object is created
 Used to initialize values

class Student:

def __init__(self, name, age):

[Link] = name

[Link] = age

s1 = Student("Kunal", 22)

print([Link])

self Keyword
 Refers to current object
 Used to access variables inside class

class Test:

def show(self):

print("Hello")

t = Test()

[Link]()
Types of Variables
✅ Instance Variable

 Defined inside constructor


 Unique for each object

[Link] = name

Class Variable

 Shared by all objects

class Student:

school = "ABC School"

Types of Methods
1. Instance Method

def display(self):

print([Link])

2. Class Method

 Uses @classmethod

@classmethod

def show_school(cls):

print([Link])
. Static Method

 Uses @staticmethod

@staticmethod

def greet():

print("Hello")

7. Encapsulation
 Wrapping data + methods together
 Restrict direct access

Access Modifiers:
Type Syntax Access

Public name Anywhere

Protected _name Within class/subclass

Private __name Only inside class

class Bank:

def __init__(self):

self.__balance = 1000 # private

def show_balance(self):

return self.__balance
8. Inheritance
Allows one class to inherit another.

class Parent:

def show(self):

print("Parent")

class Child(Parent):

pass

c = Child()

[Link]()

Types of Inheritance:

1. Single
2. Multiple
3. Multilevel
4. Hierarchical
5. Hybrid
1. Single Inheritance
One child class inherits from one parent class

class Parent:

def show(self):

print("This is Parent")

class Child(Parent):

def display(self):

print("This is Child")

c = Child()

[Link]()

[Link]()

2. Multiple Inheritance

One child class inherits from multiple parent classes

class Father:

def skills(self):

print("Gardening")

class Mother:

def skills(self):

print("Cooking")

class Child(Father, Mother):


pass

c = Child()

[Link]() # MRO decides output

3. Multilevel Inheritance
A class inherits from a class, which itself inherits from another class

class Grandparent:

def house(self):

print("Grandparent House")

class Parent(Grandparent):

pass

class Child(Parent):

pass

c = Child()

[Link]()

4. Hierarchical Inheritance
Multiple child classes inherit from a single parent class

class Parent:

def show(self):
print("Parent class")

class Child1(Parent):

pass

class Child2(Parent):

pass

c1 = Child1()

c2 = Child2()

[Link]()

[Link]()

5. Hybrid Inheritance
Combination of two or more types of inheritance

class A:

def show(self):

print("Class A")

class B(A):

pass

class C(A):

pass
class D(B, C):

pass

d = D()

[Link]()

Quick Comparison Table


Type Structure Example

Single 1→1 Parent → Child

Multiple Many → 1 Father + Mother → Child

Multilevel Chain Grandparent → Parent → Child

Hierarchical 1 → Many Parent → Child1, Child2

Hybrid Mix Combination of above

9. Polymorphism
Same function name, different behavior.

class Parent:

def show(self):

print("Parent")

class Child(Parent):

def show(self):

print("Child")
print(5 + 5) # addition

print("A" + "B") # concatenation

10. Abstraction
 Hiding implementation details
 Showing only essential features

Using ABC module:

from abc import ABC, abstractmethod

class Shape(ABC):

@abstractmethod

def area(self):

pass

11. Magic / Dunder Methods


Special methods with __

Method Use

__init__ Constructor

__str__ String representation

__len__ Length

__add__ + operator
class Test:

def __str__(self):

return "Hello Object"

t = Test()

print(t)

12. Composition
"Has-A" relationship

class Engine:

def start(self):

print("Engine started")

class Car:

def __init__(self):

[Link] = Engine()

c = Car()

[Link]()
13. Aggregation
Weak relationship (independent objects)

class Student:

def __init__(self, name):

[Link] = name

class School:

def __init__(self, student):

[Link] = student

14. Method Resolution Order (MRO)


 Defines order of method execution in inheritance

class A: pass

class B(A): pass

class C(B): pass

print([Link]())

15. super() Function


 Calls parent class methods

class Parent:

def __init__(self):

print("Parent")
class Child(Parent):

def __init__(self):

super().__init__()

print("Child")

16. Data Hiding


 Restrict access using private variables

class Test:

def __init__(self):

self.__x = 10

17. Object Lifecycle


1. Object creation → __init__
2. Object destruction → __del__

def __del__(self):

print("Object destroyed")

18. Class vs Object Summary


Feature Class Object

Definition Blueprint Instance

Memory Not allocated Allocated

Example class Car c1 = Car()

19. Advantages of OOP


 Code reusability
 Modularity
 Security
 Easy maintenance
 Real-world modeling

20. Real-Life Example

class Employee:

def __init__(self, name, salary):

[Link] = name

[Link] = salary

def show(self):

print([Link], [Link])

e1 = Employee("Kunal", 50000)

[Link]()

You might also like