3. Object-Oriented Programming

3.1 Concepts

  • Class: Blueprint for creating objects.
  • Object: Instance of a class.
  • Encapsulation: Combines data and methods.
  • Data hiding: Use of private attributes with _attribute convention.

3.2 Class Structure

class MyClass:
    def __init__(self, attr):

self._attr = attr

    def get_attr(self):
        return self._attr
  • self refers to the current object instance.

3.3 Inheritance

  • A subclass inherits attributes/methods from a superclass.
class Animal:
    def speak(self):
        print("Generic sound")
class Dog(Animal):
    def speak(self):
        print("Woof")

3.4 Polymorphism

  • Subclasses can override methods.
  • Correct method is chosen at runtime depending on the object.