{"id":88,"date":"2025-10-25T06:48:20","date_gmt":"2025-10-25T06:48:20","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=88"},"modified":"2025-12-17T07:49:10","modified_gmt":"2025-12-17T07:49:10","slug":"advanced-oop-concepts","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=88","title":{"rendered":"Lesson 22: Advanced Object-Oriented Programming (OOP) Concepts"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand advanced OOP concepts such as <strong>inheritance, polymorphism, abstraction, encapsulation, class methods, static methods, and magic methods<\/strong> in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. Review of OOP Basics<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving deeper, recall the core OOP structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n    def __init__(self, brand, color):\n        self.brand = brand\n        self.color = color\n    \n    def drive(self):\n        print(f\"{self.color} {self.brand} is driving.\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>2. Inheritance (Code Reusability)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance allows one class to inherit attributes and methods from another.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vehicle:\n    def __init__(self, brand):\n        self.brand = brand\n\n    def start(self):\n        print(f\"{self.brand} vehicle started.\")\n\nclass Car(Vehicle):  # Child class\n    def horn(self):\n        print(\"Beep! Beep!\")\n\ncar1 = Car(\"Toyota\")\ncar1.start()\ncar1.horn()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Toyota vehicle started.\nBeep! Beep!\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Types of Inheritance:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Single<\/strong> \u2192 One parent, one child<\/li>\n\n\n\n<li><strong>Multiple<\/strong> \u2192 Child inherits from multiple parents<\/li>\n\n\n\n<li><strong>Multilevel<\/strong> \u2192 Grandparent \u2192 Parent \u2192 Child<\/li>\n\n\n\n<li><strong>Hierarchical<\/strong> \u2192 One parent, multiple children<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>3. Polymorphism (Many Forms)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Same method name, different behavior depending on object type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:\n    def sound(self):\n        return \"Bark\"\n\nclass Cat:\n    def sound(self):\n        return \"Meow\"\n\nfor animal in (Dog(), Cat()):\n    print(animal.sound())\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Bark\nMeow\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd12 <strong>4. Encapsulation (Data Protection)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Encapsulation hides internal data using private attributes (<code>__<\/code> prefix).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class BankAccount:\n    def __init__(self, balance):\n        self.__balance = balance  # Private variable\n\n    def deposit(self, amount):\n        self.__balance += amount\n\n    def get_balance(self):\n        return self.__balance\n\naccount = BankAccount(1000)\naccount.deposit(500)\nprint(account.get_balance())\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1500\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf10 <strong>5. Abstraction (Hiding Complexity)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use <strong>abstract classes<\/strong> to define required methods without implementation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from abc import ABC, abstractmethod\n\nclass Shape(ABC):\n    @abstractmethod\n    def area(self):\n        pass\n\nclass Circle(Shape):\n    def __init__(self, r):\n        self.r = r\n    def area(self):\n        return 3.14 * self.r * self.r\n\ncircle = Circle(5)\nprint(circle.area())\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>78.5\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf0 <strong>6. Class Methods and Static Methods<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Class Method (<code>@classmethod<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to modify class-level data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee:\n    company = \"ABC Corp\"\n\n    @classmethod\n    def change_company(cls, new_name):\n        cls.company = new_name\n\nEmployee.change_company(\"XYZ Ltd\")\nprint(Employee.company)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>XYZ Ltd\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Static Method (<code>@staticmethod<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Doesn\u2019t use class or instance; behaves like a utility function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Math:\n    @staticmethod\n    def add(a, b):\n        return a + b\n\nprint(Math.add(5, 10))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>15\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2728 <strong>7. Magic Methods (Dunder Methods)<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Magic (or <em>dunder<\/em>) methods start and end with <code>__<\/code>.<br>They let you define behavior for built-in Python operations.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Purpose<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>__init__<\/code><\/td><td>Constructor<\/td><td>Initialization<\/td><\/tr><tr><td><code>__str__<\/code><\/td><td>String representation<\/td><td><code>print(obj)<\/code><\/td><\/tr><tr><td><code>__add__<\/code><\/td><td>Add two objects<\/td><td><code>obj1 + obj2<\/code><\/td><\/tr><tr><td><code>__len__<\/code><\/td><td>Length<\/td><td><code>len(obj)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Book:\n    def __init__(self, title, pages):\n        self.title = title\n        self.pages = pages\n    \n    def __str__(self):\n        return f\"Book: {self.title}\"\n    \n    def __add__(self, other):\n        return self.pages + other.pages\n\nbook1 = Book(\"Python Basics\", 200)\nbook2 = Book(\"OOP Advanced\", 300)\n\nprint(book1)          # Uses __str__\nprint(book1 + book2)  # Uses __add__\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Book: Python Basics\n500\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 <strong>8. Real-Life Example \u2013 Employee Management System<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee:\n    raise_percentage = 1.05\n    \n    def __init__(self, name, salary):\n        self.name = name\n        self.salary = salary\n    \n    def apply_raise(self):\n        self.salary *= self.raise_percentage\n\nclass Developer(Employee):\n    raise_percentage = 1.10\n\ndev = Developer(\"Sameer\", 50000)\ndev.apply_raise()\nprint(dev.salary)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>55000.0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand advanced OOP concepts such as inheritance, polymorphism, abstraction, encapsulation, class methods, static methods, and magic methods in Python. \ud83e\udde9 1. Review of OOP Basics Before diving deeper, recall the core OOP structure: \ud83e\uddf1 2. Inheritance (Code Reusability) Inheritance allows one class to inherit attributes and methods from another. Output: \u2705 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=88"}],"version-history":[{"count":2,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/88\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}