๐Ÿงฉ๐Ÿ’ป

Object-Oriented Programming

Learn OOP concepts step by step ๐Ÿš€

๐ŸŽ“ BCA โ€ข OBJECT-ORIENTED PROGRAMMING

๐Ÿงฉ Object-Oriented Programming

Learn Classes, Objects, Encapsulation, Inheritance, Polymorphism, Abstraction, Constructors and Exception Handling with simple examples.

๐Ÿ“š OOP Topics

๐Ÿงฉ 1. OOP Introduction

Object-Oriented Programming (OOP) is a programming approach in which programs are designed around objects and classes.

๐Ÿ’ก Simple Meaning

OOP allows us to represent real-world entities such as students, cars, employees and bank accounts using objects.

๐ŸŒ Real World
โ†’
๐Ÿงฉ Class
โ†’
๐Ÿ“ฆ Object

โญ Main OOP Concepts

๐Ÿ”’
Encapsulation
๐Ÿงฌ
Inheritance
๐Ÿ”„
Polymorphism
๐ŸŽฏ
Abstraction

๐Ÿ—๏ธ 2. Classes & Objects

๐Ÿ—๏ธ What is a Class?

A class is a blueprint or template used to create objects.

๐Ÿ“ฆ What is an Object?

An object is an instance of a class.

๐Ÿ—๏ธ CLASS

Student

Name
Roll No
Course

โ†’

๐Ÿ“ฆ OBJECT

student1

Roshni
101
BCA

๐Ÿ’ป Python Example
class Student: def display(self): print("Student Details") student1 = Student() student1.display()

๐Ÿ”’ 3. Encapsulation

Encapsulation means combining data and methods into a single unit and controlling access to the data.

๐Ÿ“ฆ Data
+
โš™๏ธ Methods
โ†’
๐Ÿ”’ Encapsulated Class
๐Ÿ’ป Example
class BankAccount: def __init__(self): self.__balance = 0 def deposit(self, amount): self.__balance += amount def get_balance(self): return self.__balance account = BankAccount() account.deposit(1000) print(account.get_balance())
โญ Key Point:

Encapsulation helps protect data and control how it is accessed.

๐Ÿงฌ 4. Inheritance

Inheritance allows a new class to acquire properties and methods from an existing class.

๐Ÿ‘จ Parent Class
โ†“
๐Ÿ‘ฉ Child Class
๐Ÿ’ป Python Example
class Animal: def eat(self): print("Eating") class Dog(Animal): def bark(self): print("Barking") dog = Dog() dog.eat() dog.bark()

๐Ÿงฌ Types of Inheritance

1๏ธโƒฃ Single Inheritance
2๏ธโƒฃ Multiple Inheritance
3๏ธโƒฃ Multilevel Inheritance
4๏ธโƒฃ Hierarchical Inheritance

๐Ÿ”„ 5. Polymorphism

Polymorphism means "many forms". The same method or operation can behave differently in different situations.

โš™๏ธ Same Method
โ†’
๐Ÿ”„ Different Behaviour
๐Ÿ’ป Example
class Dog: def sound(self): print("Bark") class Cat: def sound(self): print("Meow") animals = [Dog(), Cat()] for animal in animals: animal.sound()
๐Ÿ’ก Remember:

One interface or method name can represent different behaviours.

๐ŸŽฏ 6. Abstraction

Abstraction means hiding unnecessary implementation details and showing only the important features.

โš™๏ธ Complex Implementation
โ†’
๐ŸŽฏ Hide Details
โ†’
๐Ÿ‘ค Simple Interface
๐ŸŒ Real-Life Example:

When using an ATM, you interact with buttons and options without needing to know the internal banking system.

๐Ÿ’ป Basic Python Example
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass class Dog(Animal): def sound(self): print("Bark")

๐Ÿ—๏ธ 7. Constructors

A constructor is a special method that is automatically called when an object is created.

๐Ÿ—๏ธ Class
โ†’
๐Ÿ“ฆ Object Created
โ†’
โš™๏ธ __init__()
๐Ÿ’ป Constructor Example
class Student: def __init__(self, name, age): self.name = name self.age = age student1 = Student("Roshni", 20) print(student1.name) print(student1.age)
โญ Important:

In Python, __init__() is commonly used to initialize object attributes.

โš ๏ธ 8. Exception Handling

Exception handling is used to handle runtime errors without abruptly stopping the program.

โ–ถ๏ธ try
โ†’
โš ๏ธ Error?
โ†’
๐Ÿ› ๏ธ except
โ†’
๐Ÿงน finally
๐Ÿ’ป Example
try: number = int(input("Enter number: ")) print(10 / number) except ZeroDivisionError: print("Cannot divide by zero") except ValueError: print("Enter a valid number") finally: print("Program completed")

๐Ÿ“ OOP Quick Revision

๐ŸŽฏ BCA OOP Exam Checklist

Make sure you can explain these concepts with definitions, diagrams and examples.

โœ… OOP Basics
โœ… Classes
โœ… Objects
โœ… Encapsulation
โœ… Inheritance
โœ… Polymorphism
โœ… Abstraction
โœ… Constructors
โœ… Exception Handling