{"id":488,"date":"2025-05-19T11:36:50","date_gmt":"2025-05-19T11:36:50","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=488"},"modified":"2025-05-20T12:56:49","modified_gmt":"2025-05-20T12:56:49","slug":"object-oriented-programming-oop","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/","title":{"rendered":"Object-Oriented Programming (OOP)"},"content":{"rendered":"<h2>Classes and objects<\/h2>\n<p><strong>What Are Classes and Objects?<\/strong><\/p>\n<p>A class is a blueprint for creating objects \u2014 a way to bundle data and functionality together.<\/p>\n<p>An object is an instance of a class, representing a specific implementation of that blueprint.<\/p>\n<h3>Defining a Class<\/h3>\n<p style=\"text-align: center\"><em>class Dog:<\/em><br \/>\n<em>def __init__(self, name, breed):<\/em><br \/>\n<em>self.name = name # attribute<\/em><br \/>\n<em>self.breed = breed # attribute<\/em><\/p>\n<p style=\"text-align: center\"><em>def bark(self): # method<\/em><br \/>\n<em>print(f&#8221;{self.name} says woof!&#8221;)<\/em><\/p>\n<p><strong>Key Parts:<\/strong><\/p>\n<ul>\n<li>__init__: a constructor that runs when an object is created.<\/li>\n<li>self: refers to the current object instance.<\/li>\n<li>name and breed: attributes of the object.<\/li>\n<li>bark: a method, a function that belongs to the class.<\/li>\n<\/ul>\n<p><strong>Creating Objects (Instances)<\/strong><\/p>\n<p style=\"text-align: center\"><em>dog1 = Dog(&#8220;Buddy&#8221;, &#8220;Labrador&#8221;)<\/em><br \/>\n<em>dog2 = Dog(&#8220;Coco&#8221;, &#8220;Poodle&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>dog1.bark() # Buddy says woof!<\/em><br \/>\n<em>dog2.bark() # Coco says woof!<\/em><\/p>\n<p>Each object has its own state (data) and can use the class\u2019s methods.<\/p>\n<p><strong>Accessing and Modifying Attributes<\/strong><\/p>\n<p style=\"text-align: center\"><em>print(dog1.name) # Buddy<\/em><br \/>\n<em>dog1.name = &#8220;Max&#8221;<\/em><br \/>\n<em>print(dog1.name) # Max<\/em><\/p>\n<p><strong>Why Use Classes?<\/strong><\/p>\n<ul>\n<li>Encapsulation: bundle data + behavior.<\/li>\n<li>Reusability: define once, create many objects.<\/li>\n<li>Organization: keeps code clean and modular.<\/li>\n<li>Extensibility: easy to expand with new methods or attributes.<\/li>\n<\/ul>\n<p><strong>Example with Behavior<\/strong><\/p>\n<p style=\"text-align: center\"><em>class BankAccount:<\/em><br \/>\n<em>def __init__(self, owner, balance=0):<\/em><br \/>\n<em>self.owner = owner<\/em><br \/>\n<em>self.balance = balance<\/em><\/p>\n<p style=\"text-align: center\"><em>def deposit(self, amount):<\/em><br \/>\n<em>self.balance += amount<\/em><\/p>\n<p style=\"text-align: center\"><em>def withdraw(self, amount):<\/em><br \/>\n<em>if amount &lt;= self.balance:<\/em><br \/>\n<em>self.balance -= amount<\/em><br \/>\n<em>else:<\/em><br \/>\n<em>print(&#8220;Insufficient funds&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>account = BankAccount(&#8220;Alice&#8221;, 100)<\/em><br \/>\n<em>account.deposit(50)<\/em><br \/>\n<em>account.withdraw(30)<\/em><br \/>\n<em>print(account.balance) # 120<\/em><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<table style=\"border-collapse: collapse;width: 100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"6\">\n<thead>\n<tr style=\"background-color: #f2f2f2\">\n<th style=\"padding: 8px;text-align: left\"><strong>Concept<\/strong><\/th>\n<th style=\"padding: 8px;text-align: left\"><strong>Explanation<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 8px\"><code>Class<\/code><\/td>\n<td style=\"padding: 8px\">A template that defines the structure and behavior of objects (instances)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 8px\"><code>Object<\/code><\/td>\n<td style=\"padding: 8px\">A concrete instance created from a class, with its own data and behavior<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 8px\"><code>Attributes<\/code><\/td>\n<td style=\"padding: 8px\">Variables that store object state (defined in <code>__init__<\/code> as <code>self.attribute<\/code>)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 8px\"><code>Methods<\/code><\/td>\n<td style=\"padding: 8px\">Functions defined within a class that operate on object data (always take <code>self<\/code> parameter)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 8px\"><code>__init__()<\/code><\/td>\n<td style=\"padding: 8px\">Constructor method that initializes new objects (automatically called when creating instances)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Constructors (__init__)<\/h2>\n<h3>What Is a Constructor?<\/h3>\n<p>A constructor is a special method in a class that&#8217;s automatically called when a new object (instance) is created.<\/p>\n<p>In Python, this constructor method is named __init__().<\/p>\n<p><strong>Purpose of __init__()<\/strong><\/p>\n<ul>\n<li>To initialize attributes (variables) of a new object.<\/li>\n<li>To ensure each object starts in a well-defined state.<\/li>\n<li>To optionally accept arguments during object creation.<\/li>\n<\/ul>\n<p><strong>Basic Syntax<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Person:<\/em><br \/>\n<em>def __init__(self, name, age): # Constructor<\/em><br \/>\n<em>self.name = name<\/em><br \/>\n<em>self.age = age<\/em><\/p>\n<ul>\n<li>self: Refers to the current instance.<\/li>\n<li>name, age: Parameters used to set object-specific data.<\/li>\n<\/ul>\n<p><strong>Creating an Object<\/strong><\/p>\n<p style=\"text-align: center\"><em>p1 = Person(&#8220;Alice&#8221;, 30)<\/em><br \/>\n<em>print(p1.name) # Alice<\/em><br \/>\n<em>print(p1.age) # 30<\/em><\/p>\n<p><strong>When Person(&#8220;Alice&#8221;, 30) is called:<\/strong><\/p>\n<ul>\n<li>Python creates a new Person object.<\/li>\n<li>Calls __init__() with name=&#8221;Alice&#8221; and age=30.<\/li>\n<li>Sets self.name and self.age.<\/li>\n<\/ul>\n<p><strong>Default Values<\/strong><\/p>\n<p><strong>You can set default values for parameters:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Person:<\/em><br \/>\n<em>def __init__(self, name=&#8221;Unknown&#8221;, age=0):<\/em><br \/>\n<em>self.name = name<\/em><br \/>\n<em>self.age = age<\/em><\/p>\n<p><strong>Now you can call Person() with no arguments:<\/strong><\/p>\n<p style=\"text-align: center\"><em>p2 = Person()<\/em><br \/>\n<em>print(p2.name, p2.age) # Unknown 0<\/em><\/p>\n<p><strong>Points to Remember<\/strong><\/p>\n<table style=\"border-collapse: collapse;width: 100%;font-family: Arial, sans-serif\" border=\"1\" cellspacing=\"0\" cellpadding=\"6\">\n<thead>\n<tr style=\"background-color: #f2f2f2\">\n<th style=\"padding: 10px;text-align: left;border-bottom: 2px solid #ddd\">Feature<\/th>\n<th style=\"padding: 10px;text-align: left;border-bottom: 2px solid #ddd\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code><strong>Special method<\/strong><\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>__init__<\/code> is automatically called when creating a new object instance<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code><strong>Self parameter<\/strong><\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Refers to the current object instance (must be first parameter in method definition)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code><strong>Initialization<\/strong><\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Primary purpose is to initialize the object&#8217;s attributes and state<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code><strong>Default values<\/strong><\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Parameters can have default values to make them optional during instantiation<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px\"><strong><code>Not mandatory<\/code><\/strong><\/td>\n<td style=\"padding: 10px\">Can be omitted if no initialization logic is required (Python provides default)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Without __init__<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Empty:<\/em><br \/>\n<em>pass<\/em><\/p>\n<p style=\"text-align: center\"><em>e = Empty()<\/em><\/p>\n<p>This still works \u2014 but the object won\u2019t have any initialized attributes unless added manually.<\/p>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>__init__ is Python\u2019s constructor method, used to initialize object attributes.<\/li>\n<li>It runs automatically when an object is created.<\/li>\n<li>It makes your classes more flexible, dynamic, and useful.<\/li>\n<\/ul>\n<h2>Instance and class variables<\/h2>\n<p><strong>What Are Instance and Class Variables?<\/strong><\/p>\n<p>Instance Variables are variables that are specific to each instance (object) of a class.<\/p>\n<p>Class Variables are variables that are shared across all instances of the class. These are stored at the class level and have the same value for every object of the class.<\/p>\n<h3>1. Instance Variables<\/h3>\n<p><strong>What Are They?<\/strong><\/p>\n<ul>\n<li>Instance variables are unique to each object. Every time a new object is created, it can have its own values for instance variables.<\/li>\n<li>They are typically initialized inside the __init__() constructor.<\/li>\n<\/ul>\n<p><strong>Example<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Dog:<\/em><br \/>\n<em>def __init__(self, name, age):<\/em><br \/>\n<em>self.name = name # instance variable<\/em><br \/>\n<em>self.age = age # instance variable<\/em><\/p>\n<p style=\"text-align: center\"><em>dog1 = Dog(&#8220;Buddy&#8221;, 3)<\/em><br \/>\n<em>dog2 = Dog(&#8220;Bella&#8221;, 5)<\/em><\/p>\n<p style=\"text-align: center\"><em>print(dog1.name) # Buddy<\/em><br \/>\n<em>print(dog2.name) # Bella<\/em><\/p>\n<p>In this case, name and age are instance variables because each dog object can have different values for these attributes.<\/p>\n<h3>2. Class Variables<\/h3>\n<p><strong>What Are They?<\/strong><\/p>\n<ul>\n<li>Class variables are shared across all instances of the class.<\/li>\n<li>They are typically defined inside the class but outside of any methods.<\/li>\n<li>Class variables are often used for properties that should be common to all objects, such as a constant or counter.<\/li>\n<\/ul>\n<p><strong>Example<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Dog:<\/em><br \/>\n<em>species = &#8220;Canine&#8221; # class variable<\/em><\/p>\n<p style=\"text-align: center\"><em>def __init__(self, name, age):<\/em><br \/>\n<em>self.name = name # instance variable<\/em><br \/>\n<em>self.age = age # instance variable<\/em><\/p>\n<p style=\"text-align: center\"><em>dog1 = Dog(&#8220;Buddy&#8221;, 3)<\/em><br \/>\n<em>dog2 = Dog(&#8220;Bella&#8221;, 5)<\/em><\/p>\n<p style=\"text-align: center\"><em>print(dog1.species) # Canine<\/em><br \/>\n<em>print(dog2.species) # Canine<\/em><\/p>\n<p style=\"text-align: center\"><em># Changing class variable for the class<\/em><br \/>\n<em>Dog.species = &#8220;Dog&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em>print(dog1.species) # Dog<\/em><br \/>\n<em>print(dog2.species) # Dog<\/em><\/p>\n<ul>\n<li>species is a class variable because it is the same for all instances of Dog. If you change it at the class level, it changes for all instances automatically.<\/li>\n<\/ul>\n<p><strong>Differences Between Instance and Class Variables<\/strong><\/p>\n<table style=\"border-collapse: collapse;width: 100%;font-family: Arial, sans-serif;margin: 15px 0\" border=\"1\" cellspacing=\"0\" cellpadding=\"8\">\n<thead>\n<tr style=\"background-color: #f8f9fa\">\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #dee2e6\">Feature<\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #dee2e6\">Instance Variables<\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #dee2e6\">Class Variables<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\"><strong>Scope<\/strong><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\">Unique to each object instance<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\">Shared among all instances of the class<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\"><strong>Initialization<\/strong><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\">Defined in <code>__init__()<\/code> method using <code>self<\/code><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\">Defined directly in class body (outside methods)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\"><strong>Access<\/strong><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\">Accessed via <code>self.&lt;variable_name&gt;<\/code><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #dee2e6\">Accessed via <code>ClassName.&lt;variable_name&gt;<\/code> or <code>self.&lt;variable_name&gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px\"><strong>Usage<\/strong><\/td>\n<td style=\"padding: 12px\">Stores object-specific data (unique state)<\/td>\n<td style=\"padding: 12px\">Stores class-level data (shared state)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3. Modifying Class Variables<\/h3>\n<p>Class variables can be accessed and modified using the class name or instance:<\/p>\n<p style=\"text-align: center\"><em>class Dog:<\/em><br \/>\n<em>species = &#8220;Canine&#8221; # class variable<\/em><\/p>\n<p style=\"text-align: center\"><em>dog1 = Dog(&#8220;Buddy&#8221;, 3)<\/em><br \/>\n<em>dog2 = Dog(&#8220;Bella&#8221;, 5)<\/em><\/p>\n<p style=\"text-align: center\"><em>print(dog1.species) # Canine<\/em><\/p>\n<p style=\"text-align: center\"><em># Changing class variable using the class name<\/em><br \/>\n<em>Dog.species = &#8220;Dog&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em>print(dog1.species) # Dog<\/em><br \/>\n<em>print(dog2.species) # Dog<\/em><\/p>\n<p><strong>Warning:<\/strong><\/p>\n<p>Modifying a class variable using an instance (e.g., dog1.species = &#8220;New species&#8221;) will create an instance variable with that name, effectively shadowing the class variable.<\/p>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>Instance Variables: Unique to each object; defined inside the __init__() method using self.<\/li>\n<li>Class Variables: Shared by all objects of the class; defined directly in the class body.<\/li>\n<\/ul>\n<h2>Inheritance and polymorphism<\/h2>\n<h3>What Is Inheritance?<\/h3>\n<p>Inheritance is a way to allow a new class (child class) to inherit attributes and methods from an existing class (parent class). This promotes code reuse and allows for creating more specialized classes based on general ones.<\/p>\n<p><strong>Key Concepts:<\/strong><\/p>\n<ul>\n<li>Parent Class (or Base Class): The class being inherited from.<\/li>\n<li>Child Class (or Derived Class): The class that inherits from the parent class.<\/li>\n<\/ul>\n<p><strong>Example of Inheritance<\/strong><\/p>\n<p style=\"text-align: center\"><em># Parent Class<\/em><br \/>\n<em>class Animal:<\/em><br \/>\n<em>def __init__(self, name):<\/em><br \/>\n<em>self.name = name<\/em><\/p>\n<p><em>def speak(self):<\/em><br \/>\n<em>print(f&#8221;{self.name} makes a sound&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em># Child Class<\/em><br \/>\n<em>class Dog(Animal):<\/em><br \/>\n<em>def __init__(self, name, breed):<\/em><br \/>\n<em>super().__init__(name) # Calls the __init__ of Animal<\/em><br \/>\n<em>self.breed = breed<\/em><\/p>\n<p><em>def speak(self): # Method overriding<\/em><br \/>\n<em>print(f&#8221;{self.name} barks&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>class Cat(Animal):<\/em><br \/>\n<em>def __init__(self, name):<\/em><br \/>\n<em>super().__init__(name) # Calls the __init__ of Animal<\/em><\/p>\n<p><em>def speak(self): # Method overriding<\/em><br \/>\n<em>print(f&#8221;{self.name} meows&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em># Using the classes<\/em><br \/>\n<em>dog = Dog(&#8220;Buddy&#8221;, &#8220;Golden Retriever&#8221;)<\/em><br \/>\n<em>cat = Cat(&#8220;Whiskers&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>dog.speak() # Buddy barks<\/em><br \/>\n<em>cat.speak() # Whiskers meows<\/em><\/p>\n<p><strong>Key Points:<\/strong><\/p>\n<ul>\n<li>The child class Dog and Cat inherit from the parent class Animal.<\/li>\n<li>The Dog and Cat classes override the speak() method to provide behavior specific to dogs and cats.<\/li>\n<li>super().__init__(name): Calls the constructor (__init__()) of the parent class to initialize the name attribute.<\/li>\n<\/ul>\n<h3>What Is Polymorphism?<\/h3>\n<p>Polymorphism means &#8220;many forms&#8221;. In Python, it allows different classes to be used interchangeably if they implement the same method signature (e.g., method name and parameters), even if their internal implementation differs.<\/p>\n<p><strong>Types of Polymorphism in Python:<\/strong><\/p>\n<ol>\n<li>Method Overriding (runtime polymorphism): A child class provides a specific implementation of a method already defined in the parent class.<\/li>\n<li>Method Overloading (not directly supported in Python, but can be mimicked using default arguments).<\/li>\n<\/ol>\n<p><strong>Example of Polymorphism (Method Overriding)<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Bird(Animal):<\/em><br \/>\n<em>def __init__(self, name, color):<\/em><br \/>\n<em>super().__init__(name)<\/em><br \/>\n<em>self.color = color<\/em><\/p>\n<p><em>def speak(self): # Overriding the speak method<\/em><br \/>\n<em>print(f&#8221;{self.name} chirps&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em># Using Polymorphism: Treating objects of different types as the same type<\/em><br \/>\n<em>def make_sound(animal):<\/em><br \/>\n<em>animal.speak()<\/em><\/p>\n<p style=\"text-align: center\"><em>dog = Dog(&#8220;Buddy&#8221;, &#8220;Golden Retriever&#8221;)<\/em><br \/>\n<em>cat = Cat(&#8220;Whiskers&#8221;)<\/em><br \/>\n<em>bird = Bird(&#8220;Tweety&#8221;, &#8220;Yellow&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em># All three classes have a `speak()` method, so we can pass them to the same function<\/em><br \/>\n<em>make_sound(dog) # Buddy barks<\/em><br \/>\n<em>make_sound(cat) # Whiskers meows<\/em><br \/>\n<em>make_sound(bird) # Tweety chirps<\/em><\/p>\n<p><strong>Key Points:<\/strong><\/p>\n<ul>\n<li>The function make_sound() treats all animals the same by calling their speak() method, regardless of their specific type.<\/li>\n<li>This demonstrates polymorphism: different animal types can respond differently to the same method call.<\/li>\n<\/ul>\n<p><strong>Combining Inheritance and Polymorphism<\/strong><\/p>\n<p>Inheritance and polymorphism often go hand in hand. With inheritance, you can define common functionality in a base class, and with polymorphism, you can override it to provide specific behavior in subclasses.<\/p>\n<p><strong>Example:<\/strong> Combining Both<\/p>\n<p style=\"text-align: center\"><em>class Animal:<\/em><br \/>\n<em>def __init__(self, name):<\/em><br \/>\n<em>self.name = name<\/em><\/p>\n<p style=\"text-align: center\"><em>def make_sound(self):<\/em><br \/>\n<em>raise NotImplementedError(&#8220;Subclasses should implement this method&#8221;)<\/em><\/p>\n<p style=\"text-align: center\"><em>class Dog(Animal):<\/em><br \/>\n<em>def make_sound(self):<\/em><br \/>\n<em>return &#8220;Woof!&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em>class Cat(Animal):<\/em><br \/>\n<em>def make_sound(self):<\/em><br \/>\n<em>return &#8220;Meow!&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em># Using Polymorphism<\/em><br \/>\n<em>animals = [Dog(&#8220;Buddy&#8221;), Cat(&#8220;Whiskers&#8221;)]<\/em><\/p>\n<p style=\"text-align: center\"><em>for animal in animals:<\/em><br \/>\n<em>print(f&#8221;{animal.name}: {animal.make_sound()}&#8221;)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Buddy: Woof!<\/em><br \/>\n<em>Whiskers: Meow!<\/em><\/p>\n<p><strong>In this example:<\/strong><\/p>\n<ul>\n<li>Inheritance allows Dog and Cat to share the make_sound() method.<\/li>\n<li>Polymorphism allows each class to have its own specific implementation of make_sound().<\/li>\n<\/ul>\n<p><strong>Summary:<\/strong><\/p>\n<table style=\"border-collapse: collapse;width: 100%;font-family: Arial, sans-serif;margin: 15px 0\" border=\"1\" cellspacing=\"0\" cellpadding=\"8\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000\"><strong>Concept<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000\"><strong>Description<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><strong>Inheritance<\/strong><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Enables a new class to inherit attributes and methods from an existing class<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><strong>Polymorphism<\/strong><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Allows objects of different classes to be treated as objects of a common parent class, with different behavior based on class type<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><strong>Method Overriding<\/strong><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Child classes can provide their own implementation of a method defined in the parent class<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px\"><code><strong>super()<\/strong><\/code><\/td>\n<td style=\"padding: 12px\">Used to call methods from a parent class in the child class, typically for initialization<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Special methods (__str__, __repr__, etc.)<\/h2>\n<p><strong>What Are Special Methods?<\/strong><\/p>\n<p>Special methods in Python are those that are defined by convention to interact with built-in Python functions or operators. They always begin and end with double underscores (__method__), which is why they are called &#8220;dunder&#8221; methods (short for &#8220;double underscore&#8221;).<\/p>\n<p>These methods allow you to define custom behavior for operations like string representation, arithmetic, comparison, and more.<\/p>\n<p><strong>Commonly Used Special Methods<\/strong><\/p>\n<h3>1. __str__() \u2013 String Representation (for End-User)<\/h3>\n<ul>\n<li>Purpose: The __str__() method is used to define a human-readable string representation of an object. This is what will be returned when you call str() on an object or print the object.<\/li>\n<li>It&#8217;s meant to provide a readable description of the object for the user.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Dog:<\/em><br \/>\n<em>def __init__(self, name, age):<\/em><br \/>\n<em>self.name = name<\/em><br \/>\n<em>self.age = age<\/em><\/p>\n<p style=\"text-align: center\"><em>def __str__(self):<\/em><br \/>\n<em>return f&#8221;{self.name} is {self.age} years old&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em>dog = Dog(&#8220;Buddy&#8221;, 3)<\/em><br \/>\n<em>print(dog) # Buddy is 3 years old<\/em><\/p>\n<p>Here, when we print dog, Python calls the __str__() method to return a string representation of the object that is easy for the user to understand.<\/p>\n<h3>2. __repr__() \u2013 Official String Representation (for Developers)<\/h3>\n<ul>\n<li>Purpose: The __repr__() method is used to define the official string representation of an object. This string should ideally be a valid Python expression that, when passed to eval(), would recreate the object (though not always possible).<\/li>\n<li>It&#8217;s intended for developers and is used in the Python interpreter and for debugging.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Dog:<\/em><br \/>\n<em>def __init__(self, name, age):<\/em><br \/>\n<em>self.name = name<\/em><br \/>\n<em>self.age = age<\/em><\/p>\n<p style=\"text-align: center\"><em>def __repr__(self):<\/em><br \/>\n<em>return f&#8221;Dog(&#8216;{self.name}&#8217;, {self.age})&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em>dog = Dog(&#8220;Buddy&#8221;, 3)<\/em><br \/>\n<em>print(repr(dog)) # Dog(&#8216;Buddy&#8217;, 3)<\/em><\/p>\n<ul>\n<li>__repr__() is typically used for the developer\u2019s view of the object, providing a more detailed, unambiguous representation that could potentially recreate the object using eval().<\/li>\n<\/ul>\n<h3>3. __len__() \u2013 Length of Object (for Built-In Functions)<\/h3>\n<ul>\n<li>Purpose: The __len__() method defines how len() behaves for instances of your class.<\/li>\n<li>It should return an integer representing the &#8220;length&#8221; of the object.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Box:<\/em><br \/>\n<em>def __init__(self, items):<\/em><br \/>\n<em>self.items = items<\/em><\/p>\n<p style=\"text-align: center\"><em>def __len__(self):<\/em><br \/>\n<em>return len(self.items)<\/em><\/p>\n<p style=\"text-align: center\"><em>box = Box([1, 2, 3, 4])<\/em><br \/>\n<em>print(len(box)) # 4<\/em><\/p>\n<ul>\n<li>Here, __len__() allows us to use the len() function on an instance of Box.<\/li>\n<\/ul>\n<h3>4. __eq__() \u2013 Equality Comparison (for ==)<\/h3>\n<ul>\n<li>Purpose: The __eq__() method allows you to define how == (equality comparison) works for instances of your class.<\/li>\n<li>It should return True or False based on whether the two objects are considered equal.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Dog:<\/em><br \/>\n<em>def __init__(self, name, age):<\/em><br \/>\n<em>self.name = name<\/em><br \/>\n<em>self.age = age<\/em><\/p>\n<p style=\"text-align: center\"><em>def __eq__(self, other):<\/em><br \/>\n<em>return self.name == other.name and self.age == other.age<\/em><\/p>\n<p style=\"text-align: center\"><em>dog1 = Dog(&#8220;Buddy&#8221;, 3)<\/em><br \/>\n<em>dog2 = Dog(&#8220;Buddy&#8221;, 3)<\/em><br \/>\n<em>print(dog1 == dog2) # True<\/em><\/p>\n<p>Here, we define the == operator to compare two Dog objects based on their name and age attributes.<\/p>\n<h3>5. __add__() \u2013 Addition (for + Operator)<\/h3>\n<ul>\n<li>Purpose: The __add__() method allows you to define how the + operator behaves for instances of your class.<\/li>\n<li>This can be useful for classes that represent numeric data or collections.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Point:<\/em><br \/>\n<em>def __init__(self, x, y):<\/em><br \/>\n<em>self.x = x<\/em><br \/>\n<em>self.y = y<\/em><\/p>\n<p style=\"text-align: center\"><em>def __add__(self, other):<\/em><br \/>\n<em>return Point(self.x + other.x, self.y + other.y)<\/em><\/p>\n<p style=\"text-align: center\"><em>point1 = Point(1, 2)<\/em><br \/>\n<em>point2 = Point(3, 4)<\/em><br \/>\n<em>result = point1 + point2<\/em><br \/>\n<em>print(result.x, result.y) # 4 6<\/em><\/p>\n<p>Here, we define the + operator to add two Point objects by adding their respective x and y coordinates.<\/p>\n<h3>6. __getitem__() \u2013 Indexing (for [] Operator)<\/h3>\n<ul>\n<li>Purpose: The __getitem__() method allows you to define how the indexing operator [] works for your objects.<\/li>\n<li>It\u2019s often used in classes that represent sequences or mappings (like lists, tuples, or dictionaries).<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>class Basket:<\/em><br \/>\n<em>def __init__(self, items):<\/em><br \/>\n<em>self.items = items<\/em><\/p>\n<p style=\"text-align: center\"><em>def __getitem__(self, index):<\/em><br \/>\n<em>return self.items[index]<\/em><\/p>\n<p style=\"text-align: center\"><em>basket = Basket([1, 2, 3, 4])<\/em><br \/>\n<em>print(basket[2]) # 3<\/em><\/p>\n<p>Here, __getitem__() lets us use the indexing operator [] to retrieve an item from the Basket.<\/p>\n<p><strong>Other Useful Special Methods<\/strong><\/p>\n<ul>\n<li>__setitem__(self, key, value): Used for setting values using the [] operator.<\/li>\n<li>__delitem__(self, key): Used for deleting items using the del statement.<\/li>\n<li>__iter__(): Returns an iterator for the object (needed for iteration).<\/li>\n<li>__next__(): Defines behavior for getting the next item in an iteration.<\/li>\n<li>__call__(): Allows an instance of a class to be called like a function.<\/li>\n<\/ul>\n<p><strong>Summary of Common Special Methods<\/strong><\/p>\n<table style=\"border-collapse: collapse;width: 100%;font-family: Arial, sans-serif;margin: 15px 0\" border=\"1\" cellspacing=\"0\" cellpadding=\"8\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000\"><strong>Method<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000\"><strong>Purpose<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000\"><strong>Example Use Case<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><code>__str__()<\/code><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Provides a human-readable string representation of an object<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Used when printing or converting to string (<code>str()<\/code>)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><code>__repr__()<\/code><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Provides an official string representation of an object for developers<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Used for debugging and interpreter representations<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><code>__len__()<\/code><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Defines the behavior of the <code>len()<\/code> function for an object<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Used for classes representing collections<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><code>__eq__()<\/code><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Defines the behavior of the <code>==<\/code> comparison between objects<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Used for comparing objects<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\"><code>__add__()<\/code><\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Defines the behavior of the <code>+<\/code> operator for objects<\/td>\n<td style=\"padding: 12px;border-bottom: 1px solid #000\">Used for adding objects (like Point objects)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 12px\"><code>__getitem__()<\/code><\/td>\n<td style=\"padding: 12px\">Defines the behavior of the indexing operator <code>[]<\/code><\/td>\n<td style=\"padding: 12px\">Used for accessing elements in a custom collection<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Classes and objects What Are Classes and Objects? A class is a blueprint for creating objects \u2014 a way to bundle data and functionality together. An object is an instance of a class, representing a specific implementation of that blueprint. Defining a Class class Dog:<\/p>\n","protected":false},"author":1,"featured_media":489,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-488","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Object-Oriented Programming (OOP) - Python Course<\/title>\n<meta name=\"description\" content=\"Object-Oriented Programming (OOP) is a paradigm that organizes code into classes and objects to model real-world entities and behaviors.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Object-Oriented Programming (OOP) - Python Course\" \/>\n<meta property=\"og:description\" content=\"Object-Oriented Programming (OOP) is a paradigm that organizes code into classes and objects to model real-world entities and behaviors.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T11:36:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T12:56:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Object-Oriented-Programming-OOP.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Object-Oriented Programming (OOP)\",\"datePublished\":\"2025-05-19T11:36:50+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/\"},\"wordCount\":2395,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Object-Oriented-Programming-OOP.webp\",\"articleSection\":[\"Python Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/\",\"name\":\"Object-Oriented Programming (OOP) - Python Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Object-Oriented-Programming-OOP.webp\",\"datePublished\":\"2025-05-19T11:36:50+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"description\":\"Object-Oriented Programming (OOP) is a paradigm that organizes code into classes and objects to model real-world entities and behaviors.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Object-Oriented-Programming-OOP.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Object-Oriented-Programming-OOP.webp\",\"width\":1200,\"height\":628,\"caption\":\"Object-Oriented Programming (OOP)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/object-oriented-programming-oop\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Object-Oriented Programming (OOP)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Object-Oriented Programming (OOP) - Python Course","description":"Object-Oriented Programming (OOP) is a paradigm that organizes code into classes and objects to model real-world entities and behaviors.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/","og_locale":"en_US","og_type":"article","og_title":"Object-Oriented Programming (OOP) - Python Course","og_description":"Object-Oriented Programming (OOP) is a paradigm that organizes code into classes and objects to model real-world entities and behaviors.","og_url":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-19T11:36:50+00:00","article_modified_time":"2025-05-20T12:56:49+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Object-Oriented-Programming-OOP.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Object-Oriented Programming (OOP)","datePublished":"2025-05-19T11:36:50+00:00","dateModified":"2025-05-20T12:56:49+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/"},"wordCount":2395,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Object-Oriented-Programming-OOP.webp","articleSection":["Python Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/","url":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/","name":"Object-Oriented Programming (OOP) - Python Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Object-Oriented-Programming-OOP.webp","datePublished":"2025-05-19T11:36:50+00:00","dateModified":"2025-05-20T12:56:49+00:00","description":"Object-Oriented Programming (OOP) is a paradigm that organizes code into classes and objects to model real-world entities and behaviors.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Object-Oriented-Programming-OOP.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Object-Oriented-Programming-OOP.webp","width":1200,"height":628,"caption":"Object-Oriented Programming (OOP)"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/python\/object-oriented-programming-oop\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Python Course","item":"https:\/\/buhave.com\/courses\/learn\/python\/"},{"@type":"ListItem","position":3,"name":"Object-Oriented Programming (OOP)"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=488"}],"version-history":[{"count":1,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/488\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/488\/revisions\/490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/489"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}