diff --git a/Lesson 3 - Introduction to OOP Concepts/Abstraction.py b/Lesson 3 - Introduction to OOP Concepts/Abstraction.py new file mode 100644 index 0000000..c1c3960 --- /dev/null +++ b/Lesson 3 - Introduction to OOP Concepts/Abstraction.py @@ -0,0 +1,22 @@ +from abc import ABC, abstractmethod + +class Payment(ABC): + @abstractmethod + def pay(self, amount): + pass + +class CreditCardPayment(Payment): + def pay(self, amount): + print(f"Processing credit card payment of ₹{amount}...") + +class UPIPayment(Payment): + def pay(self, amount): + print(f"Processing UPI payment of ₹{amount}...") + +# Using the classes +payment1 = CreditCardPayment() +payment1.pay(1000) + +payment2 = UPIPayment() #Processing credit card payment of ₹1000... + +payment2.pay(500) #Processing UPI payment of ₹500... diff --git a/Lesson 3 - Introduction to OOP Concepts/Encapsulation.py b/Lesson 3 - Introduction to OOP Concepts/Encapsulation.py new file mode 100644 index 0000000..776d378 --- /dev/null +++ b/Lesson 3 - Introduction to OOP Concepts/Encapsulation.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Example: Encapsulation in Python + +class BankAccount: + def __init__(self, owner, balance): + # Private attribute (cannot be accessed directly) + self.__balance = balance + self.owner = owner + + # Public method to view balance + def get_balance(self): + return self.__balance + + # Public method to deposit money + def deposit(self, amount): + if amount > 0: + self.__balance += amount + print(f"Deposited ₹{amount}. New balance: ₹{self.__balance}") + else: + print("Deposit amount must be positive!") + + # Public method to withdraw money + def withdraw(self, amount): + if 0 < amount <= self.__balance: + self.__balance -= amount + print(f"Withdrew ₹{amount}. Remaining balance: ₹{self.__balance}") + else: + print("Invalid withdrawal amount!") + +# --- Using the class --- +account = BankAccount("Dathu", 5000) + +# Deposit some amount +account.deposit(1000) # Deposited ₹1000. New balance: ₹6000 + +# Withdraw some amount +account.withdraw(2000) # Withdrew ₹2000. Remaining balance: ₹4000 + +# Access the balance through method (not directly) +print(f"Final Balance: ₹{account.get_balance()}") # Final Balance: ₹4000 + + + + + + diff --git a/Lesson 3 - Introduction to OOP Concepts/Polymorphism.py b/Lesson 3 - Introduction to OOP Concepts/Polymorphism.py new file mode 100644 index 0000000..462af35 --- /dev/null +++ b/Lesson 3 - Introduction to OOP Concepts/Polymorphism.py @@ -0,0 +1,35 @@ +# Example of Polymorphism + +class Animal: + def speak(self): + pass # Abstract idea — every animal speaks differently + +class Dog(Animal): + def speak(self): + return "Woof!" + +class Cat(Animal): + def speak(self): + return "Meow!" + +class Cow(Animal): + def speak(self): + return "Moo!" + +# Function showing polymorphism +def make_animal_speak(animal): + print(animal.speak()) + +# Using different animal objects +dog = Dog() +cat = Cat() +cow = Cow() + +for animal in [dog, cat, cow]: + make_animal_speak(animal) + + +#OUTPUT: + # Woof! + # Meow! + # Moo! diff --git a/Lesson 3 - Introduction to OOP Concepts/oops.md b/Lesson 3 - Introduction to OOP Concepts/oops.md index bbf67ef..f05e5a6 100644 --- a/Lesson 3 - Introduction to OOP Concepts/oops.md +++ b/Lesson 3 - Introduction to OOP Concepts/oops.md @@ -89,3 +89,35 @@ print("{} is {} years old".format( woo.name, woo.age)) (Source: [https://www.geeksforgeeks.org/oop-in-python-set-3-inheritance-examples-of-object-issubclass-and-super/](https://www.geeksforgeeks.org/oop-in-python-set-3-inheritance-examples-of-object-issubclass-and-super/) Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can to derive a class from another class for a hierarchy of classes that share a set of attributes and methods. + + +## **Python Encapsulation ** + +Source: https://www.geeksforgeeks.org/encapsulation-in-python/ + +Encapsulation is one of the core principles of Object-Oriented Programming. It refers to the bundling of data (variables) and methods (functions) that operate on that data within a single unit called a class. It helps protect the internal state of an object by restricting direct access and allowing modification only through controlled interfaces like getters and setters. +In simple terms, encapsulation ensures data security and control over how attributes are accessed or modified. + +## **Python Polymorphism ** + +Source: https://www.geeksforgeeks.org/polymorphism-in-python/ + +Polymorphism means “many forms.” In Python OOP, it allows the same function name or operator to behave differently based on the object or data type it is applied to. This makes code more flexible and extensible. +It is commonly implemented through method overriding (same method name in a derived class) and method overloading (same method name with different parameters, though Python handles this differently than some languages). + +## ** Python Abstraction ** + +Source: https://www.geeksforgeeks.org/abstraction-in-python/ + +Abstraction focuses on hiding the complex implementation details and showing only the essential features of an object. +In Python, abstraction is achieved using abstract classes and abstract methods from the abc module. +It helps developers work with high-level functionality without worrying about the underlying complexity — improving code clarity and modularity. + +## **Python Classes and Objects ** + +Source: https://www.geeksforgeeks.org/python-classes-and-objects/ + +Classes and Objects form the foundation of Object-Oriented Programming in Python. +A class acts as a blueprint or template for creating objects, defining their properties (attributes) and behaviors (methods). +An object is an instance of a class that holds actual data and can perform actions defined by the class. +Together, they provide the structure that enables code reuse, organization, and real-world modeling in programming. \ No newline at end of file