DEV Community

Sajjad Rahman
Sajjad Rahman

Posted on

Core oop

āϚāĻžāϰāϟāĻŋ Core OOP Concepts āĻšāϞ⧋:

  • Encapsulation

  • Inheritance

  • Polymorphism

  • Abstraction

āϤ⧋āĻŽāĻžāϰ āĻĒā§āϰāĻœā§‡āĻ•ā§āĻŸā§‡ āϕ⧋āύāϟāĻŋ āϕ⧋āĻĨāĻžā§Ÿ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇āĻ›, āĻĻ⧇āĻ–āĻŋāĨ¤

1. OOP Core Concepts Mapping

1. Encapsulation

Included

āϕ⧋āĻĨāĻžā§Ÿ: BankAccount class

  • Account balance āϏāϰāĻžāϏāϰāĻŋ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύ āĻ•āϰāĻž āϝāĻžāĻŦ⧇ āύāĻžāĨ¤

  • _balance āĻŦāĻž __balance āĻĻāĻŋā§Ÿā§‡ attribute protect āĻ•āϰāĻžāĨ¤

  • deposit() āĻāĻŦāĻ‚ withdraw() methods āĻĻāĻŋā§Ÿā§‡ balance āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāύāĨ¤

  • @property āĻāĻŦāĻ‚ setter āĻŦā§āϝāĻŦāĻšāĻžāϰāĨ¤

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount

    def get_balance(self):
        return self.__balance
🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis 🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis

2. Inheritance

Included

āϕ⧋āĻĨāĻžā§Ÿ: Account, SavingsAccount, CurrentAccount classes

  • Account āĻšāĻŦ⧇ Parent classāĨ¤

  • SavingsAccount āĻšāĻŦ⧇ Child classāĨ¤

  • CurrentAccount āĻšāĻŦ⧇ Child classāĨ¤

  • super() āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ parent class-āĻāϰ functionality reuse āĻ•āϰāĻžāĨ¤

class Account:
    def __init__(self, balance):
        self.balance = balance

class SavingsAccount(Account):
    pass

class CurrentAccount(Account):
    pass
🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis 🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis

3. Polymorphism

Included

āϕ⧋āĻĨāĻžā§Ÿ: SavingsAccount āĻāĻŦāĻ‚ CurrentAccount

āĻāĻ•āχ method, āĻ•āĻŋāĻ¨ā§āϤ⧁ account āĻ…āύ⧁āϝāĻžā§Ÿā§€ āĻ­āĻŋāĻ¨ā§āύ behaviourāĨ¤

class SavingsAccount(Account):
    def withdraw(self, amount):
        print("Savings withdrawal rules")

class CurrentAccount(Account):
    def withdraw(self, amount):
        print("Current withdrawal rules")
🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis 🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis

āĻāĻ–āĻžāύ⧇ āĻāĻ•āχ withdraw() method āĻĻ⧁āχ āϧāϰāύ⧇āϰ account-āĻ āĻ­āĻŋāĻ¨ā§āύāĻ­āĻžāĻŦ⧇ āĻ•āĻžāϜ āĻ•āϰāĻŦ⧇āĨ¤

4. Abstraction

Included

āϕ⧋āĻĨāĻžā§Ÿ: Account abstract class

  • ABC āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŦ⧇āĨ¤

  • @abstractmethod āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŦ⧇āĨ¤

  • Child classes-āϕ⧇ āύāĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āϟ methods implement āĻ•āϰāϤ⧇ āĻŦāĻžāĻ§ā§āϝ āĻ•āϰāĻŦ⧇āĨ¤

from abc import ABC, abstractmethod

class Account(ABC):

    @abstractmethod
    def withdraw(self, amount):
        pass

    @abstractmethod
    def calculate_interest(self):
        pass
🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis 🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis

2. āϤ⧋āĻŽāĻžāϰ Project-āĻ OOP Class Structure

āĻāĻ­āĻžāĻŦ⧇ āϤ⧋āĻŽāĻžāϰ class relationship āϏāĻžāϜāĻžāύ⧋ āϝ⧇āϤ⧇ āĻĒāĻžāϰ⧇:

                 Account (ABC)
                      |
             -------------------
             |                 |
       SavingsAccount    CurrentAccount
             |                 |
             -------------------
                      |
               BankAccount
🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis 🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis

āϤāĻŦ⧇ āĻāĻ–āĻžāύ⧇ Account-āϕ⧇āχ āĻŽā§‚āϞ abstract parent class āĻšāĻŋāϏ⧇āĻŦ⧇ āϰāĻžāĻ–āĻŦ⧇; āφāϞāĻžāĻĻāĻž BankAccount parent āĻŦāĻžāύāĻžāύ⧋āϰ āĻĒā§āĻ°ā§Ÿā§‹āϜāύ āύ⧇āχāĨ¤

āĻ…āĻ¨ā§āϝāĻĻāĻŋāϕ⧇:

Customer
   |
   └── Customer Information

Transaction
   |
   └── Transaction Information

Bank
   |
   ├── Customers
   ├── Accounts
   └── Transactions
🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis 🔴 Hot Update: sajjadrahman/core oop aki - Latest Analysis

3. āϤ⧋āĻŽāĻžāϰ Project-āĻ āφāϰ āϕ⧋āύ OOP Concepts āφāϛ⧇?

āϚāĻžāϰāϟāĻŋ core pillar āĻ›āĻžā§œāĻžāĻ“ āϤ⧁āĻŽāĻŋ āφāϰāĻ“ āĻ•āĻŋāϛ⧁ āϗ⧁āϰ⧁āĻ¤ā§āĻŦāĻĒā§‚āĻ°ā§āĻŖ OOP concepts āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŦ⧇āĨ¤

Concept āϕ⧋āĻĨāĻžā§Ÿ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŦ⧇
Class & Object Customer, Account, Bank
Constructor init()
Instance Attributes Customer name, account balance
Instance Methods deposit(), withdraw()
Class Attributes Account type āĻŦāĻž shared configuration
Inheritance SavingsAccount, CurrentAccount
Encapsulation Private balance
Polymorphism Different withdrawal behaviours
Abstraction ABC, abstract methods
Method Overriding Child class-āĻāϰ āύāĻŋāϜāĻ¸ā§āĻŦ methods
super() Parent constructor/method access
@property Controlled attribute access
@classmethod Class-level operations
@staticmethod Utility methods

4. āφāĻŽāĻžāϰ Recommendation

āϤ⧋āĻŽāĻžāϰ āĻļ⧇āĻ–āĻžāϰ āϏ⧁āĻŦāĻŋāϧāĻžāϰ āϜāĻ¨ā§āϝ āĻāχ āĻ•ā§āϰāĻŽā§‡ OOP concepts implement āĻ•āϰ⧋:

  • Class & Object

  • Constructor (__init__)

  • Instance attributes and methods

  • Encapsulation

  • Inheritance

  • Method Overriding

  • Polymorphism

  • Abstraction

  • super(), @property, @classmethod, @staticmethod

āĻāĻ•āϟāĻž āϗ⧁āϰ⧁āĻ¤ā§āĻŦāĻĒā§‚āĻ°ā§āĻŖ āĻŦāĻŋāώ⧟: āϤ⧋āĻŽāĻžāϰ āĻŦāĻ°ā§āϤāĻŽāĻžāύ Project Requirements-āĻ āϚāĻžāϰāϟāĻŋ OOP pillars-āχ āĻ°ā§Ÿā§‡āϛ⧇āĨ¤ āϤāĻžāχ āύāϤ⧁āύ āĻ•āϰ⧇ OOP stage āϝ⧋āĻ— āĻ•āϰāĻžāϰ āĻĒā§āĻ°ā§Ÿā§‹āϜāύ āύ⧇āχāĨ¤ Stage 04 āĻāĻŦāĻ‚ Stage 05 āϝāĻĨ⧇āĻˇā§āϟāĨ¤

Top comments (0)